Skip to content

Incorrect sequence of middlewares when importing a router from another module #239

Description

@Lezvix

When an error occurs in a router handler that was declared in another module, the first error handler is triggered, not the one following the path declaration

How to reproduce:

// index.js
const express = require("ultimate-express");
const router = require('./router');

const app = express();
app.use(express.json());
app.set("catch async errors", true);

app.use(function jsonErrorHandler(err, req, res, next){
    if(err){
        return res.send("JSON error");
    }
    next();
})

app.use("/route", router);
app.use(function errorHandler(err, req, res, next) {
    if(err){
        return res.json(err.toString());
    }
    next();
});

app.listen(3000);
// router.js
const express = require("ultimate-express");

const router = express.Router();

router.post("/path", function handler(req, res) {
    throw new Error("Whoops");
    //return res.send("test");
});

module.exports = router;

Expected: Whoops
Actual: JSON error

In Express the sequence is correct, if you move the router to index.js the sequence is also correct

I also encountered that when an error occurred in the handler, the application returned a 404 error and ignored the error interceptor, but I can't reproduce this behavior right now

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions