grpc / grpc-dotnet

gRPC for .NET
Apache License 2.0
4.18k stars 769 forks source link

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

Open bartosz-jarmuz opened 1 year ago

bartosz-jarmuz commented 1 year ago

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.

JamesNK commented 1 year ago

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?

bartosz-jarmuz commented 1 year ago

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

JamesNK commented 1 year ago

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