1.1.1 • Published 6 years ago

sharp-error-handler v1.1.1

Weekly downloads
21
License
MIT
Repository
-
Last release
6 years ago

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
}
1.1.1

6 years ago

1.1.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.1.0

6 years ago