question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Curried Default Error Handler Failing

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
wesleytoddcommented, May 18, 2018

Also, if anyone is interested, you can do this with a simple middleware factory function:

const genericErrorHandler = (prefix) => {
  return (err, req, res, next) => {
    console.log(`${prefix} err message:`, err.message)
    res.status(500).send('Something broke!')
  }
}
app.use(genericErrorHandler('foo'))
0reactions
JesterXLcommented, May 18, 2018

BTW, those stumbling here, I found way to make this work while still allowing a longer arity for easier unit testing & acting like a curried function using default parameters.

Take the curried function:

const genericErrorHandler = curry((prefix, err, req, res, next) => {
    console.log(`${prefix} err message:`, err.message)
    res.status(500).send('Something broke!');
})

And just build a wrapper (or ditch the curried one), and move the default curried left parameters to the end with defaults:

const genericErrorHandler = (err, req, res, next, prefix='some value') => {
    console.log(`${prefix} err message:`, err.message)
    res.status(500).send('Something broke!');
})

In my case, my prefix was a function; both work, the arity is always 4:

const specialLog = ()=> {...}
const genericErrorHandler = (err, req, res, next, specialLogFunction=specialLog) => {
   ... 
   specialLogFunction(err)
   ...
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using the JavaScript Either monad for error handling
In this article, we'll take a look at using JavaScript's Either monad to handle errors as an alternative to try...catch.
Read more >
Error Handling in Large .NET Projects - Best Practices
Effective error and exception handling in any kind of an application plays an important role in providing a pleasant experience to the user, ......
Read more >
Error Handlers | MuleSoft Documentation
Mule throws a messaging error (a Mule error) whenever a problem occurs within a flow of a Mule app ... You can rely...
Read more >
GraphQL error handling to the max with Typescript, codegen ...
We'll see in this article how to: achieve a better error representation than the default offered by GraphQL; get type safety using Typescript ......
Read more >
Function currying with default params in JavaScript - LinkedIn
We defined a curried function updateProductLastView and then we use it ... Handle your default value in the body of the function itself...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found