Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including sharp-error-handler with all npm packages installed. Try it out:

var sharpErrorHandler = require("sharp-error-handler")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

sharp-error-handler v1.1.1

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

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
}

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free