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

sharp-error-handler

v1.1.1

Published

an error handler that integrates with Hapi/boom to manage all server errors through JSON file

Downloads

13

Readme

Sharp-Error-Handler

A NodeJS module that integrates with HapiJS Server Framework & uses Boom to return custom error messages, error codes & custom error fields.

First define a json object containing where the key is the error name, value is an object containing the required statusCode & message (you may use also use extraFields


{
	"invalidId": {
		"statusCode": 400,
		"message": "This Id is invalid"
	},
	
	"notAuthorized": {
		"statusCode": 403,
		"message": "You're not authorized to access this item"
	}
}

Then create your custom error objects


const errorHandler = require('sharp-error-handler');
const ServerErrors = errorHandler.createServerErrors(errorsData);

Then inside your code


throw new ServerErrors.InvalidId();

and in the catch function


catch(error) {
	return errorHandler.wrapError(null, error);
}

This makes Hapi Server returns


{
    "statusCode": 400,
    "message": "This Id is invalid",
    "error": "Bad Request Error"
}

Features

  • wrapError function takes two arguments, the first is a log class & the second is the error object itself. the module calls the log function of the first argument, this is helpful if your server uses a default logging library like winston. You can also provide the console class

  • Some modules use logging with tag, example

      
    class.log(tag, message)

    Then you can use the other function which is wrapErrorWithTag which takes three arguments. loggerClass, tag, error

  • The arrangement of arguments is made this way to facilitate the usage of curry functions.

  • a flag called isCreatedServerError is added on the error object if the user needs to know whether the error was thrown on purpose or not

      
    const wrappedError = errorHandler.wrapError(null, error);
    console.log(wrappedError.isCreatedServerError); //true if the error was created by errorHandler.createServerErrors
  • Error names are defaulted to first letter capital example: if errors.json is

      
    {
    	"invalidId": {
    		"statusCode": 400,
    		"message": "This Id is invalid"
        }
    }

    Then the error will to throw will be

      
    ServerErrors.InvalidId()
  • If data is provided in the constructor object of the error it will be added by default to the error message example: if errors.json is

      
    {
    	"invalidId": {
    		"statusCode": 400,
    		"message": "This Id is invalid: "
    	}
    }

    and the error is

      
    throw new ServerErrors.InvalidId("122");

    The message returned will be

      
    "This Id is invalid: 122"

extraFields:

  • adds all keyValue pairs written inside it to the default output object of Hapi

  • must be an object

  • example:

    errors.json

      
    {
    	"invalidId": {
    		"statusCode": 400,
    		"message": "This Id is invalid",
    		"extraFields": {
    			"ERROR_CODE": 112
    		}
    	}
    }

    Hapi final result will be

      
    {
        "statusCode": 400,
        "message": "This Id is invalid",
        "error": "Bad Request Error",
        "ERROR_CODE": 112
    }