I need to enhance the error handling process in my express application by passing two pieces of information to the error handler, which will then send both pieces of information in a JSON format to the client. Currently, I am only able to include an error message like this:
app.use((req, res, next) => {
var err = new Error('test error 1');
err.status = 404;
next(err);
});
However, I want to not only provide the error message but also specify the position where the error occurred so that the client can display the message accurately. Something along these lines:
app.use((req, res, next) => {
var err = new Error({err: {pos: 'field3', msg: 'test error 1'}});
err.status = 404;
next(err);
});
After setting up the error object with multiple properties, I plan to send this information to the client using the following code:
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.json(err.message);
});
The issue is that creating the error object only allows for a single string value for the message. How can I effectively pass multiple pieces of information (potentially comprising more than just two parts) through my error handling middleware?