How to handle the custom exceptions thrown from the gRPC interceptor.

Visto 163 veces
Saltar al primer mensaje no leído

Farhad P

no leída,
10 ago 2023, 15:31:3610/8/23
a grpc.io
I've this problem where I'm unable to capture the custom exceptions thrown by the global gRPC server side interceptor handler in the gRPC client.

   It's not clear to me as to how would one capture those custom exceptions thrown by the gRPC service call. The primary code stack in .NET 7.0 and C#.

    What is needed on the client side to display those messages from the global interceptor 
    handler?
    Between, I've defined a DomainException class, which inherits Exception class and have 
    defined errors.proto file to display the exception message along with their corresponding 
    custom code.

    Looking forward to receiving some inputs on how to display the custom exception  
    captured by the gRPC service call.

-FP.

Jan Tattermusch

no leída,
22 ago 2023, 4:29:0922/8/23
a grpc.io
I'm not sure if i'm understanding your question correctly, but it seems that you want exceptions that are thrown on the server-side to become catchable on the client. This is not how gRPC works though. Any kind of error needs to be represented in terms of an gRPC status code and a string ("error details") and that is the info that gets transferred to the client (and end up being represented on the client as RpcException).
Please refer to https://grpc.io/docs/guides/error/ for additional info (it also describes a "rich error model" scenario where you can pack more info about the error into RPC's trailers - if you are interested in that, the implementation is WIP here https://github.com/grpc/grpc-dotnet/pull/2205)

Farhad P

no leída,
22 ago 2023, 13:33:1222/8/23
a grpc.io
Thank you for getting back Jan. Yes, I later found the error handling framework for gRPC. I later found out that the gRPC Global Interceptor can be extended and the UnaryServerHandler method can be overriden. We're now using the latter approach to pack our exceptions in the API response just so that its thrown with proper tags as a JSON response.

-FP.
Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos