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

nice-http-error

v1.1.0

Published

Provides Simple interface for creating errors and associating them with an accepted http status code.

Downloads

240

Readme

Nice Custom Rest Errors

Create custom errors to supply meaningful error messages in response bodies, and clear readable logs. Nice Error extends the javascript Error class allowing for the creation of high quality custom errors.

Installation:

npm install nice-http-error --save

API:

create

create is a static class method to be used as a convenience for instantiating new instances of Nice Error.

const niceError = require('nice-http-error');
niceError.create('A problem', {
  code: 'Special Error',
  detail: 'You made a big Mistake',
  status: 'BAD_REQUEST',
  error: anErrorObject
})

Nice Error can be passed two arguments. The first argument, the "message", is required. The message should be a very short terse description of the error. The second argument is an options object. All of it's properties are optional. The options object should conform to the following contract:

 {
 code: "optional - string", // slugified-error-code, use this to lookup against localization service for display
 detail: 'optional - string', //short helpful description of the problem to aid in debugging
 error: 'optional - error object', //error object to be encapsulated into NiceError.metadata
 status: 'optional - string', //String representing an accepted nice http status i.e 'BAD_REQUEST', 'SERVICE_UNAVAILABLE'
 }

Create will return a new error object of the type NiceError. It is important to note the behavior around status. Nice Error will only enumerate http status codes from this list:

BAD_REQUEST: 400,
UNAUTHORIZED: 401,
REQUEST_FAILED: 402,
FORBIDDEN: 403,
NOT_FOUND: 404,
GONE: 410,
RESOURCE_NOT_FOUND: 420,
INTERNAL_ERROR: 500,
SERVICE_UNAVAILABLE: 503,
GATEWAY_TIMEOUT: 504

If Anything else is passed in, or if nothing is passed in at all, it will default to a Internal Server Error/500.

from:

From is a static class method to verify that your error is a nice error. If you pass it an instance of NiceError it will simply return that same instance back to you. If you pass it a regular error object, it will encapsulate it, assign it a status code of 500, and return you an instance of Nice Error.

const niceError = require('nice-http-error');
niceError.from(error)

This is important, because obviously if you where to try and check the status code property of a vanilla error object the javascript engine is going to throw an undefined error for the status property.

Recommended Usage:

You should use this any where you're trying to handle explicit errors in a micro service and would like to return a meaningful response body and clean readable logs.

Using Nice Error in actual code might look something like this:

const niceError = require('nice-http-error');
module.exports = (pool, products, routeConfig) =>
  pool.query(releaseInventorySQL, [getInventoryIds(products)])
  .catch(error => {
    throw niceError.create('MySQL Error', {
      detail: 'Error releasing inventory',
      status: 'BAD_REQUEST',
      error
    })
  });

Using it to send a response back should look something like this:

.catch(error => {
  let handledError = niceError.from(error);
  routeConfig.logger.error(handledError);
  return res.status(handledError.status).send(handledError);
});

Development

run npmm install //to get started
npm run lint:live //--continuous linting
npm run test:live //--continuous unit testing.