It appears that Express checks the function's arity to identify if it's an error-handler route. It works with a function, and works with an arrow function, but does not work with a Lodash curried function. I believe this is possibly because theFunction.length will print out 0 for curried functions, whereas function and ()=> will print out a true arity. However, I can't seem to find where in the code y'all are checking for this. I've tried to manually setting the theFunction.length to 3 or 4 to hack it, but no dice. Any pointers?
Works when you hit localhost:3000/error:
const express = require('express')
const curry = require('lodash/fp/curry')
const app = express()
app.get('/', (req, res) => res.send('Hello World!'))
app.get('/error', (req, res, next) => {
next(new Error('boom'))
})
function genericErrorHandler(err, req, res, next) {
console.log("err message:", err.message)
res.status(500).send('Something broke!');
}
app.use(genericErrorHandler)
app.listen(3000, () => console.log('Example app listening on port 3000!'))
Doesn't work:
const express = require('express')
const curry = require('lodash/fp/curry')
const app = express()
app.get('/', (req, res) => res.send('Hello World!'))
app.get('/error', (req, res, next) => {
next(new Error('boom'))
})
const genericErrorHandler = curry((prefix, err, req, res, next) => {
console.log(`${prefix} err message:`, err.message)
res.status(500).send('Something broke!');
})
const curried = genericErrorHandler('🐮')
curried.length = 4 // I tried, heh
app.use(curried);
app.listen(3000, () => console.log('Example app listening on port 3000!'))
It appears that Express checks the function's arity to identify if it's an error-handler route. It works with a function, and works with an arrow function, but does not work with a Lodash curried function. I believe this is possibly because
theFunction.lengthwill print out 0 for curried functions, whereasfunctionand()=>will print out a true arity. However, I can't seem to find where in the code y'all are checking for this. I've tried to manually setting thetheFunction.lengthto 3 or 4 to hack it, but no dice. Any pointers?Works when you hit
localhost:3000/error:Doesn't work: