npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-rescue

v1.2.0

Published

A wrapper for async functions which makes sure all async errors are passed to your errors handler

Downloads

959

Readme

JavaScript Style Guide Coverage Status CI node-current Downloads

Express Rescue

This is a dependency-free wrapper (or sugar code layer if you will) for async middlewares which makes sure all async errors are passed to your stack of error handlers, allowing you to have a cleaner and more readable code.

Note Even though this library is supposed to work on node >= 7.6, we are unable to test against node versions 7, 8 and 9 because our tooling requires at least node version 10. We'll keep the minimum requirement for this library at version node >= 7.6 so that we allow old projects to keep getting updates, but be aware that we are unable to test againt the mentioned versions.

How to use it

It's really simple:

const rescue = require('express-rescue')

/**
 * `rescue` insures thrown errors are passed to `next` callback.
 */
app.get('/:id', rescue(async (req, res, next) => {
    const user = await repository.getById(req.params.id)

    if (!user) {
      throw new UserNotFoundError
    }

    res.status(200)
       .json(user)
}))

/**
 * `rescue.from` allows you to handle a specific error which is helpful for
 * handling domain errors.
 */
app.use(rescue.from(UserNotFoundError, (err, req, res, next) => {
    res.status(404)
       .json({ error: 'these are not the droids you\'re looking for'})
})

/**
 * Your error handlers still works as expected. If an error doesn't match your
 * `rescue.from` criteria, it will find its way to the next error handler.
 */
app.use((err, req, res, next) => {
    res.status(500)
       .json({ error: 'i have a bad feeling about this'})
})

There's a helper function rescue.all([...]) in case you want to wrap several functions with rescue. With rescue.all, doing [rescue(fn1), rescue(fn2)] can be shortened to rescue.all([fn1, fn2]).

const rescue = require('express-rescue')

// Doing it like this
app.post('/products', rescue.all([
    validationFn,
    createProductFn
]))

// Is equivalent to this
app.post('/products', [
    rescue(validationFn),
    rescue(createProductFn)
])

That's all.

Hope you enjoy the async/await heaven!

Chears!

Tests

> [email protected] test
> mocha test/*.test.js --check-leaks --full-trace --use_strict --recursive

  const callable = rescue(async ([err,] req, res, next) => { })
    calls the last argument (next) with the thrown error
      ✔ All arguments are been passed to the callback
      ✔ Raises a TypeError if last argument is not a function
      ✔ callable(req, res, next) - works for routes and middlewares
      ✔ callable(err, req, res, next) - works for error handler middlewares
      ✔ callable(foo, bar, baz, foobar, foobaz, errorHandler) - should work for
        basically anything, as long as your function takes an error handler as
        the last parameter

  rescue.from(MyError, (err) => { })
    ✔ handles the error when error is instance of given constructor
    ✔ it call `next` function if error is not an instance of given constructor

  const callables = rescue.all([fn1, fn2, fn3])
    ✔ All given functions are wrapped with rescue

  8 passing (8ms)