I made several controllers and services in backend and felt a constant need of more specific error handling and sending tailored messages back in responses. Currently, there is no way to differentiate between errors thrown manually by us and those thrown automatically like type check or db error.
Importantly, while catching errors it can't be determined what kind of error it is. The desired feature is to send specific error messages and status codes to frontend so that they can be handled easily in toast etc.
One solution is to create a custom error class extending the javascript error with extra properties. Additionally, since there will be repeated logic regarding this, an error middleware should be created that sits after the controller's catch block.
I made several controllers and services in backend and felt a constant need of more specific error handling and sending tailored messages back in responses. Currently, there is no way to differentiate between errors thrown manually by us and those thrown automatically like type check or db error.
Importantly, while catching errors it can't be determined what kind of error it is. The desired feature is to send specific error messages and status codes to frontend so that they can be handled easily in toast etc.
One solution is to create a custom error class extending the javascript error with extra properties. Additionally, since there will be repeated logic regarding this, an error middleware should be created that sits after the controller's catch block.