grpc-dotnet icon indicating copy to clipboard operation
grpc-dotnet copied to clipboard

Why does throwning an exception in the server-side Interceptor not cause an exception caught in aspnetcore middleware?

Open bartosz-jarmuz opened this issue 2 years ago • 3 comments

Hello, I have trouble understanding the behavior of the GRPC interceptors. I thought it would act in a similar way as netcore middleware, i.e. that if I throw an exception in it, e.g. like that:

 public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(
        TRequest request,
        ServerCallContext context,
        UnaryServerMethod<TRequest, TResponse> continuation)
    {
            await continuation(request, context);
            throw new InvalidOperationException("FOO"); //or RpcException
    }

this exception would be caught by the middleware that is executed before the GRPC middleware, e.g.

 public async Task Invoke(HttpContext context)
        {
            try
            {
                await _next(context);
            }
            catch (Exception exception)
            {
                 //expected some kind of FOO error here
            }
        }

however, instead the await _next(context) finishes without problems and the error is only caught by the grpc client side code.

bartosz-jarmuz avatar Jul 26 '23 09:07 bartosz-jarmuz

gRPC catches and handles the error. gRPC requires the request is ended with a specific trailing header with the status.

Why do you want to catch the exception message?

JamesNK avatar Aug 03 '23 03:08 JamesNK

I had an error handling middleware with some logic (logging, mapping) and expected that the errors happening during gRPC calls also go through that.

bartosz-jarmuz avatar Aug 03 '23 06:08 bartosz-jarmuz

Should investigate whether it is worth providing an option to gRPC server to rethrow exceptions.

JamesNK avatar Aug 11 '23 01:08 JamesNK