Good morning, I am doing a REST API project using Express and with the intention of refactoring the code to return the client an error response I do not know which of the following options is more efficient:
Use a bug handler as specified in the Express guide.
app.use((err,req,res,next)=>{
console.log('ERROR HANDLER: ', err);
res.status(err.status || 500).send(err); //custom error
});
so that in each catch
of a Promise ...
function(req, res, next) {
Promise
.then( ... ) // throw error
.catch(next)
}
Or extend the response object as follows:
express.response.error_ = function(err){
this.status(err.status || 500).send(err);
}
in this way I can execute ...
function(req, res, next) {
Promise
.then( ... ) // throw error
.catch(res.error_)
}
Which of these is the best option?
Thank you very much in advance and greetings.