Hello fellow devs. I thought we might share some ideas about error handling in Express applications.
The goal: Having people share all kind of ideas could help everyone reading/discussing make their own error handling or strengthen the current one.
Requirements: One should handle cases having errors being thrown in one or more services, both their own and third party library utilization ones.
My idea:
1. Creation of StatusError such as
https://thepracticaldev.s3.amazonaws.com/i/u6rxsufyjqvplr3364da.png
2. Throwing this error in services that are part of our business domain with
meaningful statuses, such as 400, 404, 401 etc.
3. Throwing this error in services that handle consuming 3rd party API with
such status that has semantic meaning, even if it may be slightly different
from the one in API. One could also add additional info comprised of data
successfully computed before such error.
e.g. Throwing 404 while trying to find 3rd party resource connected to some
in our base even though third party API may throw different error but
with such response that corresponds 404. One could also add name of
resource from our base in message to help further solving the issue.
4. These errors could be caught in route handlers and sent further down the
pipe to error handler.
5. Error handler could call the appropriate error handling module which can do
multiple, separated actions, such as logging, special handling for some of
the errors, etc.
I'm very eager to hear your ideas.
Top comments (0)