From ed23f26d1a1f18888fa96bd3550a86d8b02c74b7 Mon Sep 17 00:00:00 2001 From: Phillip Hoff Date: Thu, 21 Sep 2023 14:59:12 -0700 Subject: [PATCH] Throw better exceptions when invalid actor interfaces are used. Signed-off-by: Phillip Hoff --- src/Dapr.Actors/Client/ActorProxyFactory.cs | 13 ++++++- test/Dapr.Actors.Test/ActorProxyTests.cs | 38 ++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/Dapr.Actors/Client/ActorProxyFactory.cs b/src/Dapr.Actors/Client/ActorProxyFactory.cs index 4a8fe3a08..4fc2224a3 100644 --- a/src/Dapr.Actors/Client/ActorProxyFactory.cs +++ b/src/Dapr.Actors/Client/ActorProxyFactory.cs @@ -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. @@ -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; @@ -77,6 +78,16 @@ public ActorProxy Create(ActorId actorId, string actorType, ActorProxyOptions op /// 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)); + } + + 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); diff --git a/test/Dapr.Actors.Test/ActorProxyTests.cs b/test/Dapr.Actors.Test/ActorProxyTests.cs index 078f4b5f6..b5db3e3fe 100644 --- a/test/Dapr.Actors.Test/ActorProxyTests.cs +++ b/test/Dapr.Actors.Test/ActorProxyTests.cs @@ -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. @@ -119,5 +119,41 @@ public void SetActorProxyFactoryDefaultOptions_ToNull_ThrowsArgumentNullExceptio action.Should().Throw(); } + + public interface INonActor + { + } + + [Fact] + public void CreateActorProxyForNonActorInterfaces() + { + var factory = new ActorProxyFactory(); + + var actorId = new ActorId("abc"); + + Assert.Throws(() => 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(() => factory.CreateActorProxy(actorId, "InternalActor")); + Assert.Throws(() => factory.CreateActorProxy(actorId, "InternalActor2")); + + Assert.Throws(() => factory.CreateActorProxy(actorId, typeof(IInternalActor), "InternalActor")); + Assert.Throws(() => factory.CreateActorProxy(actorId, typeof(IInternalActor2), "InternalActor2")); + } } }