Skip to content
Merged
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
4 changes: 3 additions & 1 deletion src/IceRpc/Internal/IceProtocolConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,9 @@ private async Task DispatchRequestAsync(IncomingRequest request, int requestId,
{
if (exception is not DispatchException dispatchException)
{
dispatchException = new DispatchException(StatusCode.InternalError, innerException: exception);
StatusCode statusCode = exception is InvalidDataException ?
StatusCode.InvalidData : StatusCode.InternalError;
dispatchException = new DispatchException(statusCode, innerException: exception);
}
response = dispatchException.ToOutgoingResponse(request);
}
Expand Down
30 changes: 30 additions & 0 deletions tests/IceRpc.Tests/IceProtocolConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,36 @@ public async Task Dispatcher_failure(
Assert.That(readResult.Buffer.IsEmpty, Is.True);
}

/// <summary>Verifies that exceptions thrown by the dispatcher are classified into the right status code:
/// InvalidDataException surfaces as StatusCode.InvalidData, anything else as StatusCode.InternalError.</summary>
[TestCase("InvalidData", StatusCode.InvalidData)]
[TestCase("Other", StatusCode.InternalError)]
public async Task Thrown_dispatcher_exception_is_classified(string exceptionKind, StatusCode expectedStatusCode)
{
// Arrange
var dispatcher = new InlineDispatcher((request, cancellationToken) => exceptionKind switch
{
"InvalidData" => throw new InvalidDataException("boom"),
_ => throw new InvalidOperationException("boom")
});

await using ServiceProvider provider = new ServiceCollection()
.AddProtocolTest(Protocol.Ice, dispatcher)
.BuildServiceProvider(validateScopes: true);
var sut = provider.GetRequiredService<ClientServerProtocolConnection>();
await sut.ConnectAsync();
using var request = new OutgoingRequest(new ServiceAddress(Protocol.Ice) { Path = "/foo" })
{
Operation = "op"
};

// Act
IncomingResponse response = await sut.Client.InvokeAsync(request);

// Assert
Assert.That(response.StatusCode, Is.EqualTo(expectedStatusCode));
}

/// <summary>Verifies that a StatusCode dispatched by the server is encoded as a ReplyStatus and decoded back to
/// the expected StatusCode by the client.</summary>
[Test, TestCaseSource(nameof(StatusCodeRoundTripSource))]
Expand Down
2 changes: 1 addition & 1 deletion tests/IceRpc.Tests/ProtocolConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private static IEnumerable<TestCaseData> DispatcherThrowsExceptionSource
}

yield return new(Protocol.IceRpc, new InvalidDataException("invalid data"), StatusCode.InvalidData);
yield return new(Protocol.Ice, new InvalidDataException("invalid data"), StatusCode.InternalError);
yield return new(Protocol.Ice, new InvalidDataException("invalid data"), StatusCode.InvalidData);

yield return new(
Protocol.IceRpc,
Expand Down
Loading