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

yhttperror

v8.0.0

Published

Better HTTP errors for your NodeJS server.

Downloads

977

Readme

yhttperror

Better HTTP errors for your NodeJS server.

GitHub license Coverage Status

Usage

As a child constructor of the YError one, YHTTPError allow you to get more valuable errors for better debugging, even for production errors.

YHTTPError also allows you to specify an HTTP error code, with 500 as a fallback. Here is a sample use case near to how we use it at SimpliField:

import YHTTPError from 'yhttperror';

app.put('/users/:user_id', function(req, res, next) {
  // Let's start a Promise chain
  Promise.resolve()
  // This validate the payload an could throw errors
  .then(checkUserUpdatePayload.bind(null, req.body))
  // wrap any error into an HTTP 400 error with debug params
  .catch(function wrapUserPayloadError(err) {
    throw YHTTPError.wrap(err, 400, 'E_BAD_PAYLOAD', req.params.user_id, req.body);
  })
  // This check rights an return the result
  .then(checkUserRights.bind(null, req.user._id, 'USER_UPDATE'))
  // throw a YHTTPError if the user can't update an user
  .then(throwIfNotAuthorized(authorized) {
    if(!authorized) {
      throw new YHTTPError(401, 'E_UNAUTHORIZED', 'USER_UPDATE', authorized);
    }
  })
  // This update the user in the database
  .then(updateUser.bind(null, req.params.user_id, req.body))
  // wrap the error only if it is not yet a YHTTPError, in this case, db errors
  .catch(function castDatabaseError(err) {
    throw YHTTPError.cast(err, 500, 'E_DB_ERROR');
  })
  .then(sendUser.bind(null, res))
  .catch(function sendError(err) {
    // Sending the error, better done in an error middleware but keeping things
    // simple here. We're just passing the error to next like this in production
    // code: .catch(next);
    res.status(err.httpCode).send(err.stack);
  });
});

The above would give you valuable errors like this:

// Sample payload error:
// YError: E_INVALID_PROPERTY ('name', undefined)
//    at validateUser (/validateUser.js:5:11)
//    (...)
// YHTTPError [400]: E_BAD_PAYLOAD ('abbacacaabbacacaabbacaca', { email: '[email protected]' })
//    at Function.YHTTPError.wrap (/home/nfroidure/simplifield/yhttperror/src/index.js:41:12)
//    at /home/nfroidure/simplifield/yhttperror/test.js:16:21
//    (...)

// Sample right error:
// YHTTPError [401]: E_UNAUTHORIZED ('b17eb17eb17eb17eb17eb17e', 'USER_UPDATE')
//    at Function.YHTTPError.wrap (/home/nfroidure/simplifield/yhttperror/src/index.js:41:12)
//    at /home/nfroidure/simplifield/yhttperror/test.js:16:21
//    (...)

// Sample db error:
// Error: Cannot connect to database
//    at dbConnect (/db.js:5:11)
//    (...)
// YHTTPError [500]: E_DB_ERROR ()
//    at Function.YHTTPError.wrap (/home/nfroidure/simplifield/yhttperror/src/index.js:41:12)
//    at /home/nfroidure/simplifield/yhttperror/test.js:16:21
//    (...)

API

YHTTPError ⇐ Error

Class representing an HTTP Error with extra debug informations

Kind: global class
Extends: Error, YError

new YHTTPError(httpCode, [errorCode], [...params])

Creates a new YHTTPError with an HTTP error code, an error code and some params as debug values.

| Param | Type | Default | Description | | --- | --- | --- | --- | | httpCode | number | 500 | The HTTP error code corresponding to the actual error | | [errorCode] | string | "'E_UNEXPECTED'" | The error code corresponding to the actual error | | [...params] | any | | Some additional debugging values |

YHTTPError.wrap(err, httpCode, [errorCode], [...params]) ⇒ YHTTPError

Wraps any error and output a YHTTPError with an HTTP error code, an error code and some params as debug values.

Kind: static method of YHTTPError
Returns: YHTTPError - The wrapped error

| Param | Type | Default | Description | | --- | --- | --- | --- | | err | Error | | The error to wrap | | httpCode | number | | The HTTP error code corresponding to the actual error | | [errorCode] | string | "'E_UNEXPECTED'" | The error code corresponding to the actual error | | [...params] | any | | Some additional debugging values |

YHTTPError.cast(err, httpCode, [errorCode], [...params]) ⇒ YHTTPError

Return YHTTPError as is or wraps any other error and output a YHTTPError with an HTTP error code, an error code and some params as debug values.

Kind: static method of YHTTPError
Returns: YHTTPError - The wrapped error

| Param | Type | Default | Description | | --- | --- | --- | --- | | err | Error | | The error to cast | | httpCode | number | | The HTTP error code corresponding to the actual error | | [errorCode] | string | "'E_UNEXPECTED'" | The error code corresponding to the actual error | | [...params] | any | | Some additional debugging values |

YHTTPError.bump(err, httpCode, [errorCode], [...params]) ⇒ YHTTPError

Same than YHTTPError.wrap() but preserves the HTTP code, the error code and the debug values of the error if it is already an instance of the YHTTPError constructor.

Kind: static method of YHTTPError
Returns: YHTTPError - The wrapped error

| Param | Type | Default | Description | | --- | --- | --- | --- | | err | Error | | The error to bump | | httpCode | number | | The HTTP error code corresponding to the actual error | | [errorCode] | string | "'E_UNEXPECTED'" | The error code corresponding to the actual error | | [...params] | any | | Some additional debugging values |

Authors

License

MIT