Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw better exceptions when invalid actor interfaces are used. #1148

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
WhitWaldo marked this conversation as resolved.
Show resolved Hide resolved
using System.Net.Http;
using Dapr.Actors.Builder;
using Dapr.Actors.Communication;
Expand Down Expand Up @@ -77,6 +78,16 @@
/// <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"));
}
}
}