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 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 18 additions & 7 deletions 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 @@ -11,14 +11,15 @@
// limitations under the License.
// ------------------------------------------------------------------------

using System;
using System.Globalization;
using System.Net.Http;
using Dapr.Actors.Builder;
using Dapr.Actors.Communication;
using Dapr.Actors.Communication.Client;

namespace Dapr.Actors.Client
{
using System;
using System.Net.Http;
using Dapr.Actors.Builder;
using Dapr.Actors.Communication;
using Dapr.Actors.Communication.Client;

/// <summary>
/// Represents a factory class to create a proxy to the remote actor objects.
/// </summary>
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));
}

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
50 changes: 43 additions & 7 deletions 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 All @@ -11,14 +11,14 @@
// limitations under the License.
// ------------------------------------------------------------------------

using System;
using System.Text.Json;
using Dapr.Actors.Test;
using FluentAssertions;
using Xunit;

namespace Dapr.Actors.Client
{
using System;
using System.Text.Json;
using Dapr.Actors.Test;
using FluentAssertions;
using Xunit;

/// <summary>
/// Test class for Actor Code builder.
/// </summary>
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"));
}
}
}
Loading