Skip to content

Commit

Permalink
Demonstrate the exception that can't be received by client, issue #105.
Browse files Browse the repository at this point in the history
  • Loading branch information
yallie committed Dec 10, 2024
1 parent 5d057e4 commit d916926
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
44 changes: 44 additions & 0 deletions CoreRemoting.Tests/RpcTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,50 @@ public void NonSerializableError_method_throws_Exception()
}
}

[Fact]
public void AfterCall_event_handler_can_translate_exceptions_to_improve_diagnostics()
{
// replace cryptic database error report with a user-friendly error message
void AfterCall(object sender, ServerRpcContext ctx)
{
var errorMsg = ctx.Exception?.Message ?? string.Empty;
if (errorMsg.StartsWith("23503:"))
ctx.Exception = new Exception("Deleting clients is not allowed.",
ctx.Exception);
}

_serverFixture.Server.AfterCall += AfterCall;
try
{
using var client = new RemotingClient(new ClientConfig()
{
ConnectionTimeout = 5,
InvocationTimeout = 5,
SendTimeout = 5,
Channel = ClientChannel,
MessageEncryption = false,
ServerPort = _serverFixture.Server.Config.NetworkPort
});

client.Connect();

// simulate a database error on the server-side
var proxy = client.CreateProxy<ITestService>();
var ex = Assert.Throws<RemoteInvocationException>(() =>
proxy.Error("23503: delete from table 'clients' violates foreign key constraint 'order_client_fk' on table 'orders'"))
.GetInnermostException();

Assert.NotNull(ex);
Assert.Equal("Deleting clients is not allowed.", ex.Message);
}
finally
{
// reset the error counter for other tests
_serverFixture.ServerErrorCount = 0;
_serverFixture.Server.AfterCall -= AfterCall;
}
}

[Fact]
public void Failing_component_constructor_throws_RemoteInvocationException()
{
Expand Down
10 changes: 5 additions & 5 deletions CoreRemoting/ClientRpcContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ internal ClientRpcContext()
UniqueCallKey = Guid.NewGuid();
WaitHandle = new EventWaitHandle(initialState: false, EventResetMode.ManualReset);
}

/// <summary>
/// Gets the unique key of RPC call.
/// </summary>
public Guid UniqueCallKey { get; }

/// <summary>
/// Gets or sets the result message, that was received from server after the call was invoked on server side.
/// </summary>
public MethodCallResultMessage ResultMessage { get; set; }

/// <summary>
/// Gets or sets whether this RPC call is in error state.
/// </summary>
Expand All @@ -37,12 +37,12 @@ internal ClientRpcContext()
/// Gets or sets an exception that describes an error that occurred on server side RPC invocation.
/// </summary>
public RemoteInvocationException RemoteException { get; set; }

/// <summary>
/// Gets a wait handle that is set, when the response of this RPC call is received from server.
/// </summary>
public EventWaitHandle WaitHandle { get; } // TODO: replace with a Task?

/// <summary>
/// Frees managed resources.
/// </summary>
Expand Down

0 comments on commit d916926

Please sign in to comment.