and let express error handler handle the errors.
In my handler function I wanted to send different error messages based on the type of the error.
export const send: express.ErrorRequestHandler = (error, _req, res, _next) => {
// If it's axios error show a generic error
const message = error.response && error.response.status
? 'An error occurred.'
: error.message;
res.status(422).json({ error: message });
};
You should probably special case the expected errors, and just replace those
I'd create asyncMiddleware function that takes your handler function as a paramater and call the handler in try block and produce the error in the catch block.
You should just write a message mapper I think
Обсуждают сегодня