Why does throwning an exception in the server-side Interceptor not cause an exception caught in aspnetcore middleware?
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.
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?
I had an error handling middleware with some logic (logging, mapping) and expected that the errors happening during gRPC calls also go through that.
Should investigate whether it is worth providing an option to gRPC server to rethrow exceptions.