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

catch-async-wrapper-express

v2.0.0

Published

middleware for handling exceptions inside of async express routes and passing them to your express or custom error handlers

Downloads

125

Readme

catch-async-wrapper-express

a simple middleware for handling exceptions inside of async express routes and passing them to your express or custom error handlers.

Installation

npm i catch-async-wrapper-express
const catchAsync = require('catch-async-wrapper-express')

Requirements:

Examples:

catch any async or sync error and handle it to the next() function. having the second argument as undefined, catchAsync will default to the express next function.

// if failed, next will be called with the error
app.get(
  '/users',
  catchAsync(async (req, res, next) => {
    const data = await UserModel.find({})
    res.status(200).json({
      status: 'success',
      data,
    })
  })
)

You can also have a custom error handler as well by passing it as the second parameter:

// if failed, localErrorHandler will be called with the error

const localErrorHandler = (error, req, res, next) => {
  console.log(error)
  res.status(500).json({
    status: 'fail',
    message: 'Unable to create user, please try again',
  })
}

app.post(
  '/users',
  catchAsync(async (req, res, next) => {
    const data = await UserModel.create(req.body) // ex: using mongoose
    res.status(200).json({
      status: 'success',
      data,
    })
  }, localErrorHandler)
)

a local error handler can also be an asyncronous function as well, and calling next inside of the custom error handler will move the error to your express global error handler middleware to process.

// if failed, localErrorHandler will be called with the error

const localErrorHandler = async (error, req, res, next) => {
  console.log('returning the money back...')
  const invoice = await Invoices.findById(req.invoiceId)
  await transferMoney(invoice.to, invoice.from)
  res.status(500).json({
    status: 'fail',
    message:
      "You can't buy the item because it doesn't exist in the merchant stock, please try  again",
  })
}

app.post(
  '/purchase-item',
  // verify & deduct/feed balances
  catchAsync(async (req, res, next) => {
    const from = '[email protected]'
    const to = '[email protected]'
    const invoiceId = await transferMoney(from, to)
    req.invoiceId = invoiceId
    next()
  }),
  // verify & remove from stock
  catchAsync(async (req, res, next) => {
    await Stock.pick(req.body.itemToPurchase)
    res.status(200).json({
      status: 'success',
      data,
    })
  }, localErrorHandler)
)

If a local error handler throw an error, the error will be handled inside your express global error handler middleware to process it.

const localErrorHandler = (error, req, res, next) => {
  throw error
}

app.post(
  '/users',
  catchAsync(async (req, res, next) => {
    const data = await UserModel.create(req.body)
    res.status(200).json({
      status: 'success',
      data,
    })
  }, localErrorHandler)
)

calling next with no arguemnts inside the local error handler will get the request back on the track of the middleware stack again. This means that you handled your error, and the request is ready to continue its journy:

const localErrorHandler = async (error, req, res, next) => {
  console.log('handling the error...')
  await ErrorModel.create(error) // save the error to database
  next()
}

app.post(
  '/users',
  catchAsync(async (req, res, next) => {
    throw new Error('Hello from the error!') // an error
  }, localErrorHandler),
  (req, res, next) => {
    res.status(200).json({
      status: 'success',
      message: 'the operation was completed successfuly with (1) errors',
    })
  }
)