UNPKG

sharp-error-handler

Version:

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

148 lines (109 loc) 3.27 kB
## Sharp-Error-Handler A NodeJS module that integrates with [HapiJS Server Framework](https://hapijs.com/) & uses [Boom](https://www.npmjs.com/package/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](#extrafields) ```json { "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 ```javascript const errorHandler = require('sharp-error-handler'); const ServerErrors = errorHandler.createServerErrors(errorsData); ``` Then inside your code ```javascript throw new ServerErrors.InvalidId(); ``` and in the catch function ```javascript catch(error) { return errorHandler.wrapError(null, error); } ``` This makes Hapi Server returns ```json { "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 ```javascript 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 ```javascript 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 ```json { "invalidId": { "statusCode": 400, "message": "This Id is invalid" } } ``` Then the error will to throw will be ```javascript 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 ```json { "invalidId": { "statusCode": 400, "message": "This Id is invalid: " } } ``` and the error is ```javascript 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 ```json { "invalidId": { "statusCode": 400, "message": "This Id is invalid", "extraFields": { "ERROR_CODE": 112 } } } ``` Hapi final result will be ```json { "statusCode": 400, "message": "This Id is invalid", "error": "Bad Request Error", "ERROR_CODE": 112 } ```