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

responserror

v1.0.1

Published

Error Handler Middlware for Express

Downloads

702

Readme

Responserror

Express Error Handler to Node.js apps

Responserror is an Error Handler middleware for express.

License: MIT npm version Coverage Status Downloads

npm

CircleCI

Installation

The latest version is available at: https://www.npmjs.com/package/responserror

Use your favorite package manager to install. For instance:

  yarn add responserror

Then import it and initialize a new instance:

import Responserror from 'responserror'
const { errorHandler } = new Responserror()

Then, you will want to add the errorHandler as your last express middleware:

app.use(authRouter, userRouter, errorHandler)

And you're good to go!

Usage Example

import express from 'express'
import Responserror from 'responserror'

const app = express()
const router = express.Router()

const { errorHandler } = new Responserror()

router.post('/users', (_, response: Response, next: NextFunction) => {
  try {
    throw {
      code: 504,
    }
  } catch(err) {
    return next(err)
  }
})

app.use(router, errorHandler)

Outputs HTTP Status 504 and following JSON response:

{
  code: 504,
  status: 'GATEWAY_TIMEOUT',
  message: 'Gateway Timeout',
  success: false
}
  • code: if given responserror will try to find its status and message automatically.

  • status: if given responserror will try to find its code and message automatically.

  • success: will always be false, unless specified otherwise.

  • message: will be filled automatically if given code or status are valid and no message value is given.

  • errors: anything can be sent here, responserror will not try to fill this.

Note: if message is given a value, that will override the automatic value responserror would give. This applies to all other properties.

All properties are optional as shown in the first example.

In this example, we send message and errors as well:

router.post('/users', (_, response: Response, next: NextFunction) => {
  try {
    throw {
      code: 400,
      message: 'Sorry!! Your request is invalid! =/',
      errors: [
        { name: 'clientFullName', message: 'The full name needs to contain more than one word!' }
      ]
    }
  } catch(err) {
    return next(err)
  }
})

Outputs HTTP Status 400 and following JSON response:

{
  code: 400,
  status: 'BAD_REQUEST',
  message:  'Sorry!! Your request is invalid! =/',
  errors: [
    { name: 'clientFullName', message: 'The full name needs to contain more than one word!' }
  ]
}

Hooks

Use method pre to execute functions before sending the response:


const app = express()

app.use(responser)

const responserror = new Responserror({ promptErrors: true })

const errorHandler = responserror.errorHandler

responserror.pre(() => {
  console.info('This will execute before the error response is given!')
})

const router = express.Router()

app.use(router, errorHandler)

While pre will execute as the first thing responserror will do, method pos will execute as the last thing responserror will do before sending the response.

Status & Codes

100 // Continue
101 // Switching Protocols
102 // Processing
200 // OK
201 // Created
202 // Accepted
203 // Non Authoritative Information
204 // No Content
205 // Reset Content
206 // Partial Content
207 // Multi-Status
300 // Multiple Choices
301 // Moved Permanently
302 // Moved Temporarily
303 // See Other
304 // Not Modified
305 // Use Proxy
307 // Temporary Redirect
308 // Permanent Redirect
400 // Bad Request
401 // Unauthorized
402 // Payment Required
403 // Forbidden
404 // Not Found
405 // Method Not Allowed
406 // Not Acceptable
407 // Proxy Authentication Required
408 // Request Timeout
409 // Conflict
410 // Gone
411 // Length Required
412 // Precondition Failed
413 // Request Entity Too Large
414 // Request-URI Too Long
415 // Unsupported Media Type
416 // Requested Range Not Satisfiable
417 // Expectation Failed
418 // I'm a teapot
419 // Insufficient Space on Resource
420 // Method Failure
422 // Unprocessable Entity
423 // Locked
424 // Failed Dependency
428 // Precondition Required
429 // Too Many Requests
431 // Request Header Fields Too Large
451 // Unavailable For Legal Reasons
500 // Internal Server Error
501 // Not Implemented
502 // Bad Gateway
503 // Service Unavailable
504 // Gateway Timeout
505 // HTTP Version Not Supported
507 // Insufficient Storage
511 // Network Authentication Required

Check the updated list of http status codes for all status and codes available.

Responser

If you want to have all HTTP responses at the tip of your finger (including sucessful ones), be sure to check out responser npm package. Differently from responserror which is the "catch-all" error handler for express, responser is used to directly send responses, wherever middleware/controller you are. Both responser and responserror can be used together.

vscode suggestions

If you are using responser module in the same express instance, responserror will invoke send_* methods instead of its own.

Testing

Run the test suit with yarn test.

Contributing

If you want to contribute in any of theses ways:

  • Give your ideas or feedback
  • Question something
  • Point out a problem or issue
  • Enhance the code or its documentation
  • Help in any other way

You can (and should) open an issue or even a pull request!

Thanks for your interest in contributing to this repo!

Author

Luiz Felipe Zarco ([email protected])

License

This code is licensed under the MIT License. See the LICENSE.md file for more info.