mirror of https://github.com/dapr/dotnet-sdk.git
Adding Custom ActorMethodInvocationException and throw this new exception (#45)
* Adding Actor Custom Exception Class * Adding custom Actions error code ActionsErrorCodes.ERR_INVOKE_ACTOR for the class * Throw Custom ActorMethodInvocationException to preserve the remote exception stack * Adding Unit Test for ActorMethodInvocationException
This commit is contained in:
parent
0a13a73088
commit
719b411ec9
|
|
@ -198,17 +198,20 @@ namespace Microsoft.Actions.Actors
|
|||
var isDeserialzied =
|
||||
RemoteException.ToException(
|
||||
responseMessageBody,
|
||||
out var e);
|
||||
out var remoteMethodException);
|
||||
if (isDeserialzied)
|
||||
{
|
||||
throw e;
|
||||
throw new ActorMethodInvocationException(
|
||||
"Remote Actor Method Exception",
|
||||
remoteMethodException,
|
||||
false /* non transient */);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ServiceException(e.GetType().FullName, string.Format(
|
||||
throw new ServiceException(remoteMethodException.GetType().FullName, string.Format(
|
||||
CultureInfo.InvariantCulture,
|
||||
SR.ErrorDeserializationFailure,
|
||||
e.ToString()));
|
||||
remoteMethodException.ToString()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,46 @@
|
|||
// ------------------------------------------------------------
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
|
||||
// ------------------------------------------------------------
|
||||
|
||||
namespace Microsoft.Actions.Actors
|
||||
{
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Exception for Remote Actor Method Invocation.
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class ActorMethodInvocationException : ActionsException
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ActorMethodInvocationException"/> class.
|
||||
/// </summary>
|
||||
public ActorMethodInvocationException()
|
||||
: base(ActionsErrorCodes.ERR_INVOKE_ACTOR, false)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ActorMethodInvocationException"/> class.
|
||||
/// </summary>
|
||||
/// <param name="message">The error message that explains the reason for the exception.</param>
|
||||
/// <param name="isTransient">True, if the exception is to be treated as an transient exception.</param>
|
||||
public ActorMethodInvocationException(string message, bool isTransient)
|
||||
: base(message, ActionsErrorCodes.ERR_INVOKE_ACTOR, isTransient)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ActorMethodInvocationException"/> class with a specified error
|
||||
/// message and a reference to the inner exception that is the cause of this exception.
|
||||
/// </summary>
|
||||
/// <param name="message">The error message that explains the reason for the exception.</param>
|
||||
/// <param name="innerException">The exception that is the cause of the current exception.</param>
|
||||
/// <param name="isTransient">True, if the exception is to be treated as an transient exception.</param>
|
||||
public ActorMethodInvocationException(string message, Exception innerException, bool isTransient)
|
||||
: base(message, innerException, ActionsErrorCodes.ERR_INVOKE_ACTOR, isTransient)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
// ------------------------------------------------------------
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
|
||||
// ------------------------------------------------------------
|
||||
|
||||
namespace Microsoft.Actions.Actors.Test
|
||||
{
|
||||
using System;
|
||||
using System.IO;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Microsoft.Actions.Actors.Communication;
|
||||
using FluentAssertions;
|
||||
|
||||
[TestClass]
|
||||
public class ActorMethodInvocationExceptionTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void TestThrowActorMethodInvocationException()
|
||||
{
|
||||
// This test will test
|
||||
// 1) the path for serialization and deserialization of the remote exception
|
||||
// 2) and validating the inner exception
|
||||
|
||||
// Create Remote Actor Method test Exception
|
||||
var exception = new InvalidOperationException();
|
||||
var message = "Remote Actor Exception";
|
||||
|
||||
// Create Serialized Exception
|
||||
var serializedException = RemoteException.FromException(new InvalidOperationException());
|
||||
|
||||
// De Serialize Exception
|
||||
var isDeserialzied = RemoteException.ToException(
|
||||
new MemoryStream(serializedException),
|
||||
out var remoteMethodException);
|
||||
isDeserialzied.Should().BeTrue();
|
||||
var ex = ThrowRemoteException(message, remoteMethodException);
|
||||
ex.Should().BeOfType<ActorMethodInvocationException>();
|
||||
ex.InnerException.Should().BeOfType<InvalidOperationException>();
|
||||
ex.Message.Should().Be(message);
|
||||
}
|
||||
|
||||
private Exception ThrowRemoteException(string message, Exception exception)
|
||||
{
|
||||
return new ActorMethodInvocationException(message, exception, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -9,6 +9,7 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentAssertions" Version="5.9.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="1.3.2" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="1.3.2" />
|
||||
|
|
|
|||
Loading…
Reference in New Issue