1.0.9 • Published 7 years ago

generic-error-creator v1.0.9

Weekly downloads
1
License
ISC
Repository
-
Last release
7 years ago

Generic Error Creator

Introduction

Error creator for creating custom made and common HTTP errors for node application. Each http error contains the http status code. The user can define the error code for the application to identify.

Installation

npm install generic-error-creator --save

Code Samples

Use specific error:

const errorCreator = require('generic-error-creator').customHttpErrorsCreator;

//No need to change status it got the default type of http error status
function (req, res, next) {    
    let errMsg = "Error Route Not Found";
    let section = "Routes";
    let code = 9404; //error code for the client to identify the error
    let params = {url: req.url};
    
    let err = new errorCreator.NotFound(errMsg, section, code, params);
    next(err);
}

Make all errors global

require('generic-error-creator').initGlobalErrors();

console.log(new InternalServerError("Internal Server Error", "Core", 9500)); 
InternalServerError: Interl Server Error
  at /Users/Username/WebstormProjects/Project/app.js:12:15
  at Layer.handle [as handle_request] (/Users/Username/WebstormProjects/Project/node_modules/express/lib/router/layer.js:95:5)
  at next (/Users/Username/WebstormProjects/Project/node_modules/express/lib/router/route.js:131:13)
  at Route.dispatch (/Users/Username/WebstormProjects/Project/node_modules/express/lib/router/route.js:112:3)
  at Layer.handle [as handle_request] (/Users/Username/WebstormProjects/Project/node_modules/express/lib/router/layer.js:95:5)
  at /Users/Username/WebstormProjects/Project/node_modules/express/lib/router/index.js:277:22
  at Function.process_params (/Users/Username/WebstormProjects/Project/node_modules/express/lib/router/index.js:330:12)
  at next (/Users/Username/WebstormProjects/Project/node_modules/express/lib/router/index.js:271:10)
  at expressInit (/Users/Username/WebstormProjects/Project/node_modules/express/lib/middleware/init.js:33:5)
code: 9500,
section: 'Core',
uuid: '8a77fc8b-601c-4fbb-89df-570ced4fa42b'

Custom Error

Before changing the 'this', need to call the super method so the new extended error could be changed.

const genericErrorCreator = require('generic-error-creator');
const ErrorCreator = require('generic-error-creator').customHttpErrorsCreator;

class BadLoginRequest extends ErrorCreator.BadRequest { constructor(msg, section, code, reason){ reason = reason.toUpperCase(); super(msg, section, code, {reason: reason}); this.name = "BadLoginRequest"; } }

const customErrors = { BadLoginRequest };

genericErrorCreator.initGlobalErrors(customErrors);

console.log(new BadLoginRequest("Bad Login Request!", "SpecialLogin", 9876, "User is not special enough"));

### Convert Error To Object To Send To The Client

const app = express(); const genericErrorCreator = require('generic-error-creator'); const customErrors = { CustomError: require('./errors/custom-error') };

genericErrorCreator.initGlobalErrors(customErrors);

app.use(genericErrorCreator.prettifyErrorMiddleware);

app.get('/', function (req, res, next) { let err = new CustomError("Custom Error!!", "Custom", 9876, "User is not custom enough"); next(err); });

app.use(function (err, req, res, next) { res.status(err.status).json(err); });

1.0.9

7 years ago

1.0.9-np

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago