Skip to content
Advertisement

NestJs transform GRPC exception to HTTP exception

I have a HTTP server that connects to a gateway over GRPC. the gateway also connects to other . GRPC microservices. the flow looks like this:

Client -> HttpServer -> GRPC server (gateway) -> GRPC microservice server X

The way i handle errors currently is like so (please let me know if there is better practice) i will only show nessaccery code for brevity

GRPC microservice server X

JavaScript

this simple throws an error to the GRPC client (which works fine)

GRPC Server

JavaScript

Grpc server catches the error which is in turn caught buy the global exception handler (this works fine)

JavaScript

This throws the error back to the Http server (grpc client, works fine)

Now when it gets to the Http server i was hoping i could set up another RPC exception handler and transform the error into a HTTP except. but i’m unsure if it is possible, i have only been using nest for a few days and am yet to full understand it.

Here is an example of what i was hoping to do (code is not working, just example of what i want). id prefer to globally catch the exceptions rather than have try/catch blocks everywhere

JavaScript

Advertisement

Answer

I have been stuck at the same place for some time now. What seems to work is that only the string you send as message gets received at the HTTP server. So the code below as a filter in HTTP server works, but you have to check for status via the message string.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement