I created a function for handling the error but I have to pass this function to every try-catch block of the API. How can I avoid that? Is there a better way to handle API errors in NextJS?
The function which I created, please suggest any doc or example
const errorHandler = (err, req, res, next) => {
const statusCode = err.statusCode || 500;
const isDevelopment = process.env.NODE_ENV === 'development';
// console.log("err",err);
const responseMessage = isDevelopment ? `invalid request ${err.message}` : 'Something went wrong';
return res.status(statusCode).json({
status: false,
message: responseMessage,
});
};
module.exports = errorHandler;