Overview
This node module helps you to create your own errors with your defined error codes and handle them within classes in a proper way.
Features
- Creating errors objects instance of
Error
class from the self-defined error codes - Throw the created error any time with informative error message/code and error stack
- Parse any error to HTTP error codes and messages
- Usage with express middleware
- See custom and informative error messages in the client-side of the REST in development environment
ECMAScript support
ES6 only
Installation
$ npm install errorme
API
Check the JSDoc here.
Installation
npm install errorme --save
Usage
You can add your custom errors or use the default errors.
//defining our custom errorsconst errors = "ValidationError": "CODE": 100 "DEFAULT_MESSAGE": "The provided data is not valid" "HTTP_CODE": 400 "ServiceError": "CODE": 101 "DEFAULT_MESSAGE": "Error happend related to the third party service" "HTTP_CODE": 500 //overwriting defaults errors and requiring to show logs once an error createdlet options = overwrite: true showLogs: true //requiring modulelet errorme = errors options //getting errorlet code = 100 customMessage = "The provided data is invalid"; let err = errorme //custom message will be visible if process.env.DEV=trueconsole //true//parsing error to httplet httpErr = errconsole //400console //100console //"Bad request" //create http errorlet newHttpErr = errorme;console //trueconsole //true
Examples
To understand the real use case it will be better to see it with async module, especially with async/waterfall
let waterfall = ;let errorme = ; let { } ;
Using with express
let express = ;let app = ;let errorme = ; //creates middlewareerrorme; app; app;
Next features
- Will be ability to add other "error languages"
- Optimization for working with error codes defined by external service providers
License
MIT