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-bonfire

v0.3.2

Published

An utility and middleware for dispatch and handling errors on express.js

Downloads

12

Readme

express-bonfire

An utility and middleware for dispatch and handling errors on express.js

We dispatch something like:

dispatchError('404 | The user is not found')

On the response we have a json with http status code 404

{
  error: true,
  message: 'The user is not found'
}

Instead of return the json error manually like:

app.get('/user:id', (req, res, next) => {
  try {
    if (req.params.id !== 123) {
      return res
        .status(404)
        .json({
          error: true,
          message: 'The user is not found'
        })
    }
  } catch (error) {
    next(error)
  }
})
// Convert this:
app.get('/user:id', (req, res, next) => {
  try {
    if (req.params.id !== 123) {
      return res
        .status(404)
        .json({
          error: true,
          message: 'The user is not found'
        })
    }
  } catch (error) {
    next(error)
  }
})

// To this:
app.get('/user:id', (req, res, next) => {
  try {
    if (req.params.id !== 123) {
      dispatchError('404 | The user is not found')
    }
  } catch (error) {
    next(error)
  }
})

Installation

npm install -save express-bonfire

Usage

// Basic example
const app = require('express')()
const { dispatchError, errorMiddleware } = require('express-bonfire')

app.use(express.urlencoded({ extended: false }))
app.use(express.json())

app.get('/user/:id', (req, res, next) => {
  try {
    // Dispatch error
    dispatchError(`404 | The user with id: ${req.params.id} is not found`)
  } catch (error) {
    next(error)
  }
})

// Adding the error middleware to the end
app.use(errorMiddleware)

// In the response we have:
// status http 404 
// {
//   error: true,
//   message: 'The user with id: 123 is not found'
// }
// Example
const app = require('express')()
const { dispatchError, errorMiddleware } = require('express-bonfire')

app.use(express.urlencoded({ extended: false }))
app.use(express.json())

app.get('/login', (req, res, next) => {
  try {
    if (!req.body.email) {
      dispatchError('422 | Email was not included')
    }

    if (!req.body.password) {
      dispatchError('422 | Password was not included')
    }

    if (!req.body.password.length < 6) {
      dispatchError('422 | The password must be at least 6 characters')
    }
  } catch (error) {
    next(error)
  }
})

// Adding the error middleware to the end
app.use(errorMiddleware)

Default separators

The default separators to split the code status http and message are |, =>, -> but you can chage, creating a custom dispatcherError function.

code

You can pass code inside of parentheses eg: (7891) for handling responses on client-side

app.get('/login', (req, res, next) => {
  try {
    if (!req.body.email) {
      dispatchError('422 | Email was not included (8749)')
    }
  } catch (error) {
    next(error)
  }
})

// JSON Response: 422
// Now on client-side you can handling the errorCode for 8749
{
  "message": "Email was not included",
  "errorCode": 8749,
  "error": true
}

Extra Data

// Example
const app = require('express')()
const { dispatchError, errorMiddleware } = require('express-bonfire')

app.use(express.urlencoded({ extended: false }))
app.use(express.json())

app.get('/login', (req, res, next) => {
  try {
    if (!req.body.email) {
      dispatchError('422 | Email was not included', {
        // Other data here!
        codeErrorPath: 'email'
      })
    }
  } catch (error) {
    next(error)
  }
})

// Adding the error middleware to the end
app.use(errorMiddleware)

// In the response we have:
//  {
//    error: true,
//    message: 'Email was not included',
//    codeErrorPath: 'email'
//  }

Custom Error Dispatcher

// Example
const app = require('express')()
const { DispatchErrorCustom, errorMiddleware } = require('express-bonfire')

app.use(express.urlencoded({ extended: false }))
app.use(express.json())

// dding custom dispatch error
const customDispatch = DispatchErrorCustom({
  statusHTTP: 200,
  // Error message for humans
  message: 'Ohh no! 🔥',
  // Error code for apis
  errorCode: 100,
  dividers: [ '**', '>>' ]
})

app.get('/login', (req, res, next) => {
  try {
    if (!req.body.email) {
      customDispatch()
    }
  } catch (error) {
    next(error)
  }
})

// Adding the error middleware to the end
app.use(errorMiddleware)

// In the response we have:
//  {
//    error: true,
//    message: 'Ohh no! 🔥',
//    errorCode: 100
//  }

Custom dispatcher options

Prop Name | Type | Default | Description | ------------------ | --------- | ---------- | ----------- | statusHTTP | Number | 200 | Status code http for response | errorCode | Number | 0 | Code for handling responses on client-side | message | String |'Something is wrong 🔥'| Error message for humans | dividers | Array | ['|', '=>', '->'] | Characters for separate the status code http and message eg "404 => Whoops! Nos found" | codePattern | Object RegExp | /((\s+)?(\d*)(\s+)?)/gm | Pattern to extract the errorCode | cleanCodePattern | Object RegExp | /((|))/gm | Pattern to remove error Code to the message |


This package uses Standard JS

JavaScript Style Guide