Curried Default Error Handler Failing
See original GitHub issueIt 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:
- Created 5 years ago
- Comments:6 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Also, if anyone is interested, you can do this with a simple middleware factory function:
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:
And just build a wrapper (or ditch the curried one), and move the default curried left parameters to the end with defaults:
In my case, my prefix was a function; both work, the arity is always 4: