I'm building RESTful API with node.js and express framework and I'm looking for a way to build single error handling middleware. My current approach is:

```
router.get('/some-resource', (req, res, next) => {
  // some logic that resolves to BAD REQUEST
  const error = new Error('Bad request example message')
  error.code = 400; // should I do this?
  next(error);
});

```

My error handling middleware is the following:

```
const env = process.env.NODE_ENV;
const ErrorHandler = (error, req, res, next) => {
  const {code, stack} = error;
  if (env === 'production')
    return res.sendStatus(code || 500);
  res.status(code).send(stack);
};

module.exports = ErrorHandler;

```

Should I just assign HTTP error codes to the **Error** object's instance or is it considered a bad pracice? Should response code be specified at the route level? Is there a better way to do this?