Skip to content

Commit

Permalink
Throw better exceptions when invalid actor interfaces are used.
Browse files Browse the repository at this point in the history
  • Loading branch information
philliphoff committed Sep 21, 2023
1 parent 99d874a commit 433a1ff
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/Dapr.Actors/Client/ActorProxyFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// Copyright 2021 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -14,6 +14,7 @@
namespace Dapr.Actors.Client
{
using System;
using System.Globalization;
using System.Net.Http;
using Dapr.Actors.Builder;
using Dapr.Actors.Communication;
Expand Down Expand Up @@ -77,6 +78,16 @@ public ActorProxy Create(ActorId actorId, string actorType, ActorProxyOptions op
/// <inheritdoc/>
public object CreateActorProxy(ActorId actorId, Type actorInterfaceType, string actorType, ActorProxyOptions options = null)
{
if (!actorInterfaceType.IsAssignableFrom(actorInterfaceType))
{
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, "The actor interface type '{0}' must implement IActor.", actorInterfaceType), nameof(actorInterfaceType));

Check warning on line 83 in src/Dapr.Actors/Client/ActorProxyFactory.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Actors/Client/ActorProxyFactory.cs#L83

Added line #L83 was not covered by tests
}

if (!actorInterfaceType.IsVisible)
{
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, "The actor interface type '{0}' must be public.", actorInterfaceType), nameof(actorInterfaceType));
}

options ??= this.DefaultOptions;

var daprInteractor = new DaprHttpInteractor(this.handler, options.HttpEndpoint, options.DaprApiToken, options.RequestTimeout);
Expand Down
38 changes: 37 additions & 1 deletion test/Dapr.Actors.Test/ActorProxyTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// Copyright 2021 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -119,5 +119,41 @@ public void SetActorProxyFactoryDefaultOptions_ToNull_ThrowsArgumentNullExceptio

action.Should().Throw<ArgumentNullException>();
}

public interface INonActor
{
}

[Fact]
public void CreateActorProxyForNonActorInterfaces()
{
var factory = new ActorProxyFactory();

var actorId = new ActorId("abc");

Assert.Throws<ArgumentException>(() => factory.CreateActorProxy(actorId, typeof(INonActor), "NonActor"));
}

internal interface IInternalActor : IActor
{
}

internal interface IInternalActor2 : IInternalActor
{
}

[Fact]
public void CreateActorProxyForNonPublicActorInterfaces()
{
var factory = new ActorProxyFactory();

var actorId = new ActorId("abc");

Assert.Throws<ArgumentException>(() => factory.CreateActorProxy<IInternalActor>(actorId, "InternalActor"));
Assert.Throws<ArgumentException>(() => factory.CreateActorProxy<IInternalActor2>(actorId, "InternalActor2"));

Assert.Throws<ArgumentException>(() => factory.CreateActorProxy(actorId, typeof(IInternalActor), "InternalActor"));
Assert.Throws<ArgumentException>(() => factory.CreateActorProxy(actorId, typeof(IInternalActor2), "InternalActor2"));
}
}
}

0 comments on commit 433a1ff

Please sign in to comment.