Use named functions for error handlers
Inspired by expressjs/express#2896, but proposed here because as a breaking change it likely won't make it into Express 4.0 anyway, and Express 5.0 references this.
Instead of using the number of arguments to differentiate between error and normal handlers, is there any reason not to use named functions? Example:
route.get(function(req, res, next) {
next('oops')
}, function(err, req, res, next) {
res.end('Error')
})
Instead, we could say:
route.get(function(req, res, next) {
next('oops')
}, function error(err, req, res) {
res.end('Error')
})
And then just check for this.name instead of fn.length in layer.js:63.
Should work in all places where error handlers can be specified here and in express unless I'm overlooking something, and would remove the need (and warnings) for unused parameters.
Could even still allow the old style (but make it deprecated?) to not to break existing code.
Thoughts? :)
Personally, named functions are even more error prone than checking arrity. There's no advantage to the change. Also, it's pretty common to have an error handler that still passes on to next so we'd still need to handle three arguments. If there were to be a change to error handling, I'd suggest it uses less magic and not more.
It's probably also worth noting that this proposal would be incompatible with inline arrow functions. Right now the only way to get the name property set is through a previous assignment statement.