Closed alexandrubau closed 10 months ago
Tagging some issues that might be related: #5710 and #6496.
One way to get around this is to create a service that wraps the this.client.send<number>(pattern, payload);
from here and throw a custom exception on which you can add the message pattern and message body. Still, there is no way to get the exception from the server.
In my opinion this behaviour should be happening by default.
Errors are intentionally swallowed and auto-converted to InternalServerException
(IF the error thrown is not an instance of RpcException
class, or an instance of a class that inherits from RpcException
). In your minimum reproduction repository, you're just throwing the Error
object and so it's mapped to the InternalServerException
object (it works similarly for HTTP apps).
Why this double standard when treating exceptions?
If I throw exception (throw new Error('MyException');
) from AppA:AppController:getHello() I get a beautiful stack trace but from AppA:AppController:add() I get a generic "Internal server error" without any trace.
I do believe the behaviour should be the same in both cases.
@alexandrubau do you throw the error inside the add
method inside app A or inside the method from within app B that gets called by app A? It would be a difference of where the error happens then, right?
@jmcdo29 I, as a developer, should be able to identify the root cause of an exception, no matter the place from where it is being thrown. How should I be able to identify which of the microservice requests is failing?
In order to have some insights about where this might happen, every NestJS developer would have to find a workaround such as using a wrapper.
I mean, if you call an API (a weather API for example) and they return an error, do you expect your server to know why that API failed? Only if its in the returned data about the error, right? Microservices are no different, in my opinion, they are APIs that should return data about why they failed if they expect their consumers to react to why it failed.
If your microservice throws an RcpException
, Nest forwards that on as expected, as it does with HttpException
s in HTTP servers. If you just throw new Error()
, the the microservice will log the stack trace and return the fact that there was an unknown exception, just like the HTTP server would, right? Once in the HTTP server, the error should be handled or converted to a format Nest recognizes (HttpException
) else it gets treated like your usual Error
class would
Thank you for the explanations.
Is there an existing issue for this?
Current behavior
When developing Nest applications using microservices architecture it is common to issue requests from client to server. Some of those requests may fail for whatever reason (e.g. the server throws an exception) and the client will throw a very vague exception saying
Internal server error
without any additional information of why it failed.Minimum reproduction code
https://github.com/alexandrubau/nestjs-microservice-client-error
Steps to reproduce
nest start app-b
nest start app-a
http://localhost:3000/add
from browserapp-a
Expected behavior
Instead of throwing a very vague exception saying
Internal server error
, the client should throw a more detailed exception. Those details may aid in debugging and figuring out which external request failed. Some of the details that may be included: message pattern, message body, exception from the server, etc.Package
Other package
No response
NestJS version
10.2.0
Packages versions
Node.js version
20.7.0
In which operating systems have you tested?
Other
No response