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

maestro-express-async-errors

v1.3.0

Published

A simple an secure layer of code for async middlewares.

Downloads

30

Readme

node-current JavaScript Style Guide CI RELEASE Downloads cov

Maestro for ExpressJS Async Errors

🏆 The async/await heaven!

Maestro is a slightly layer of code that acts as a wrapper, without any dependencies, for async middlewares. Its purpose is to ensure that all errors occurring in async operations are properly passed to your stack of error handlers. By doing so, Maestro helps to improve the readability and cleanliness of your code.

Installation:

npm install --save maestro-express-async-errors

or

yarn add maestro-express-async-errors

Note The minimum supported Node version for Maestro is 7.6.0.

How to use it

With maestro:

const { maestro } = require('maestro-express-async-errors');


express.get('/', maestro(async (req, res, next) => {
	const bar = await foo.findAll();
	res.send(bar)
}))

Without maestro:

express.get('/',(req, res, next) => {
    foo.findAll()
    .then ( bar => {
       res.send(bar)
     } )
    .catch(next); // error passed on to the error handling route
})

So Easy right? 😉

Now let I show you more exemples and functionalities:

  • maestro insures thrown errors are passed to next callback:
const { maestro } = require('maestro-express-async-errors');

app.get('/:id', maestro(async (req, res, next) => {
    const user = await repository.getById(req.params.id)

    if (!user) {
      throw new UserNotFoundError
    }

    res.status(200)
       .json(user)
}))
  • maestro.from allows you to handle a specific error which is helpful for handling domain driven errors.

app.use(maestro.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 `maestro.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 maestro.all([...]) in case you want to wrap several functions with maestro. With maestro.all, doing [maestro(fn1), maestro(fn2)] can be shortened to maestro.all([fn1, fn2]).
const maestro = require('maestro-express-async-errors')

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

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

Import in Typescript:

import { maestro } from "maestro-express-async-errors"

Test Cases

> [email protected] test
> mocha --require ts-node/register test/**/*.ts



  Try maestro(async (req, res, next) => { next() or Error }) :
    Basic functionality:
      ✔ Maestro is a function.
      ✔ Maestro returns a function.
      ✔ Maestro returns a function that returns a promise.
      ✔ When an asynchronous function passed into it throws an error, it is expected that the calls next with that error.
      ✔ When a non-asynchronous function passed into it throws an error, it is expected that calls next with that error.
      ✔ Should invoke `next` when passing a non-async function.
      ✔ Thenables are not guaranteed to have a `catch` method. This test refers to this.
    Should invoke `next` with the thrown error:
      ✔ If an async function is passed as an argument, the next function should be called with the provided arguments.
      ✔ Works like a charm if all arguments are passed to the callback function.
      ✔ Raises a TypeError if next args it's not a function.
      ✔ A valid call works for routes and middlewares.
      ✔ A valid call works for error handler middlewares.

  Try maestro.from(RealProblems, (err) => { }) :
    ✔ Handles the error when error is instance of given constructor.
    ✔ It call `next` function if error is not an instance of given constructor.

  Try maestro.all([...args]) :
    ✔ Raises a TypeError when `next` it's not function.
    ✔ Should return an array.
    ✔ Should return an array of functions
    ✔ Should return an array of functions that returns a promise.
    ✔ Should return an array of functions that returns a promise that calls next.
    ✔ Should return an array of functions that returns a promise that calls next with the error.


  20 passing (75ms)