Skip to content

Commit 433a1ff

Browse files
committed
Throw better exceptions when invalid actor interfaces are used.
1 parent 99d874a commit 433a1ff

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/Dapr.Actors/Client/ActorProxyFactory.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ------------------------------------------------------------------------
1+
// ------------------------------------------------------------------------
22
// Copyright 2021 The Dapr Authors
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -14,6 +14,7 @@
1414
namespace Dapr.Actors.Client
1515
{
1616
using System;
17+
using System.Globalization;
1718
using System.Net.Http;
1819
using Dapr.Actors.Builder;
1920
using Dapr.Actors.Communication;
@@ -77,6 +78,16 @@ public ActorProxy Create(ActorId actorId, string actorType, ActorProxyOptions op
7778
/// <inheritdoc/>
7879
public object CreateActorProxy(ActorId actorId, Type actorInterfaceType, string actorType, ActorProxyOptions options = null)
7980
{
81+
if (!actorInterfaceType.IsAssignableFrom(actorInterfaceType))
82+
{
83+
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, "The actor interface type '{0}' must implement IActor.", actorInterfaceType), nameof(actorInterfaceType));
84+
}
85+
86+
if (!actorInterfaceType.IsVisible)
87+
{
88+
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, "The actor interface type '{0}' must be public.", actorInterfaceType), nameof(actorInterfaceType));
89+
}
90+
8091
options ??= this.DefaultOptions;
8192

8293
var daprInteractor = new DaprHttpInteractor(this.handler, options.HttpEndpoint, options.DaprApiToken, options.RequestTimeout);

test/Dapr.Actors.Test/ActorProxyTests.cs

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ------------------------------------------------------------------------
1+
// ------------------------------------------------------------------------
22
// Copyright 2021 The Dapr Authors
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -119,5 +119,41 @@ public void SetActorProxyFactoryDefaultOptions_ToNull_ThrowsArgumentNullExceptio
119119

120120
action.Should().Throw<ArgumentNullException>();
121121
}
122+
123+
public interface INonActor
124+
{
125+
}
126+
127+
[Fact]
128+
public void CreateActorProxyForNonActorInterfaces()
129+
{
130+
var factory = new ActorProxyFactory();
131+
132+
var actorId = new ActorId("abc");
133+
134+
Assert.Throws<ArgumentException>(() => factory.CreateActorProxy(actorId, typeof(INonActor), "NonActor"));
135+
}
136+
137+
internal interface IInternalActor : IActor
138+
{
139+
}
140+
141+
internal interface IInternalActor2 : IInternalActor
142+
{
143+
}
144+
145+
[Fact]
146+
public void CreateActorProxyForNonPublicActorInterfaces()
147+
{
148+
var factory = new ActorProxyFactory();
149+
150+
var actorId = new ActorId("abc");
151+
152+
Assert.Throws<ArgumentException>(() => factory.CreateActorProxy<IInternalActor>(actorId, "InternalActor"));
153+
Assert.Throws<ArgumentException>(() => factory.CreateActorProxy<IInternalActor2>(actorId, "InternalActor2"));
154+
155+
Assert.Throws<ArgumentException>(() => factory.CreateActorProxy(actorId, typeof(IInternalActor), "InternalActor"));
156+
Assert.Throws<ArgumentException>(() => factory.CreateActorProxy(actorId, typeof(IInternalActor2), "InternalActor2"));
157+
}
122158
}
123159
}

0 commit comments

Comments
 (0)