Skip to content

Curried Default Error Handler Failing #3657

@JesterXL

Description

@JesterXL

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!'))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions