express-catch-handler

`express-catch-handler` is a function for catching errors thrown from your request handlers and passing the error to Express `next(err)`.

Usage no npm install needed!

<script type="module">
  import expressCatchHandler from 'https://cdn.skypack.dev/express-catch-handler';
</script>

README

README

express-catch-handler is a function for catching errors thrown from your request handlers and passing the error to Express next(err).

Examples

import withCatch from 'express-catch-handler'

// bad
app.get('/', (req, res, next) => {
    try {
        // ...your throwable code...
    } catch (err) {
        next(err)
    }
})

// better
app.get('/', withCatch((req, res) => {
    // your throwable code
}))

You can customize what error is thrown in the event that your code throws literal undefined:

import withCatch, { setDefault } from 'express-catch-handler'

setDefault(new Error('foobar'))

app.get('/', withCatch((req, res) => {
    throw undefined
}))