Skip to content

Route param error handle #2483

@cosmosgenius

Description

@cosmosgenius
var webapps = express.Router();
module.exports = webapps;

function handleError(err, req, res, next) {
    //this function is not executed
    res.send("hello");
    next();
}

webapps.param("app", function(req, res, next, name) {
    next("asfaf");
});

webapps.route("/:app")
    .all(handleError);

I am expected handleError to be executed and send a response of "hello", but instead "asfaf" is being send with status 500.

Activity

dougwilson

dougwilson commented on Jan 1, 2015

@dougwilson
Contributor

Ah, it's because the error handler is within the route and since the error is the parameter, the route is not actually entered.

I agree though, that it would make sense to execute error handlers within the route if there was an error in a parameter for the route.

self-assigned this
on Jan 1, 2015
cosmosgenius

cosmosgenius commented on Jan 1, 2015

@cosmosgenius
Author

So if we have to catch this type of error it has to be on top of the router?

dougwilson

dougwilson commented on Jan 1, 2015

@dougwilson
Contributor

Yes, it has to be on the router, after your route. You can also add a second route with the same path that does not use the same parameter to try and only capture the correct error.

This change would be a pretty big behavior change, so it'll likely have to wait for 5.0, which is due this month sometime after the io.js release.

dougwilson

dougwilson commented on Jan 1, 2015

@dougwilson
Contributor

I'll put this in the router module though, so you can use that in any version of express, so you don't have to wait.

scottcorgan

scottcorgan commented on Jan 14, 2015

@scottcorgan
Member

So does this remove the error middleware with 4 params?

dougwilson

dougwilson commented on Jan 14, 2015

@dougwilson
Contributor

No. This is to make it possible to catch errors from app.param(name, fn) functions within the route itself.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @dougwilson@scottcorgan@cosmosgenius

      Issue actions

        Route param error handle · Issue #2483 · expressjs/express