So I need to handle the http errors 500, 404, 401 and 403 plus every other error needs to return the 404 status code. So far I've tried with a switch case but it doesn't seem to work.
I could get it to work with a single error but adding multiple arrors breaks even that single one. In case you were wondering yes, i did read the docs but it talks about single error handling and i really need a way to catch them globally so that i don't have to put a middleware in every single request
Any help would be appreciated
import express from 'express';
import 'dotenv/config';
//import atlasConnection from './database/mongo-connection.js';
//import erroHandler from './middlewares/error.middleware.js';
//await atlasConnection();
const port = process.env.PORT;
const app = express();
// app.use(posts);
// app.use(users);
if (process.env.NODE_ENV !== 'test') {
  app.listen(port, () => console.log(`Server listening on port ${port}`));
}
app.get('/api', (req, res, next) => {
  res.send('test')
})
app.use(app.router)
app.use(function(err, req, res, next ) {
  switch (err.status) {
    case 500:
      res.status(500).json({
        "success": false,
        "code": 4001,
        "error": "Server Error"
      });
      break;
    case 404:
      res.status(404).json({
        "success": false,
        "code": 4001,
        "error": "Resource not found"
      });
      break;
    case 401:
      res.status(401).json({
        "success": false,
        "code": 2001,
        "error": "Unauthorized"
      });
      break;
    case 403:
      res.status(403).json({
        "success": false,
        "code": 2002,
        "error": "Forbidden"
      });
      break;
    default:
      res.status(404).json({
        "success": false,
        "code": 4001,
        "error": "Resource not found"
      });
      break;
  }
  next(err)
})
export default app;
How can i make it work?
