feat: propagate framework errors to user client application #685
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, errors that happen in the framework express layer are passed to the default express error handler. The default error handler returns the error to the client with a stack trace if in non-prod enviornments. It would be useful for certain cases for the user to have context of what happened during a request instead of the user being bypassed entirely. Instead, if the user has an error handler middleware installed in their application, we should pass the framework error to the user's error handler.
For example, if the framework gets a request with a bad json body.
The framework responds with the following not very helpful message from the default Express error handler:
This change introduces a propagate error to client middleware that when enabled, propagates errors to the user client application error middleware chain, where we give power to the user to handle the error, log details, or do whatever they want to do:
The framework responds with a user defined message from the user's error handler!:
Don't forget the user also logged this error, giving them more visibility and control of their application. I think this is a useful change for users who want a bit more autonomy.