Code Monkey home page Code Monkey logo

extend-error's Introduction

extend-error

Ever tried to create custom error types in Node.js and wished it should be this simple?

var MyError = Error.extend('MyError');
throw MyError('wow')

installation

npm install extend-error

and in your app.js, just require('extend-error'). It will provide you an extend() method for the Error type.

syntax

  • extend() takes two arguments : subTypeName & errorCode [optional]
  • it returns the newly created error type

more examples for a web app

something useful

var AppError = Error.extend('AppError', 500);
var ClientError = Error.extend('ClientError', 400);

extend ClientError further for specific http types

var HttpNotFound = ClientError.extend('HttpNotFoundError', 404);
var HttpUnauthorized = ClientError.extend('HttpUnauthorized', 401);

throwing errors

throw new AppError('unable to connect db due to error: ' + err);

throw new ClientError({'message':'required field missing', field: 'email'})

throw new HttpNotFound('no post found with id: ' + id);

throw new HttpNotFound({'message': 'no such post', 'id': id});

don't worry when you forget 'new'

throw ClientError('bad request');

instanceof

throw an error in controller

var err = HttpNotFound('user profile not found');

throw err; 
(or)
callback(err)

handle it easily in global error handler (in case of express.js error middleware)

if (err instanceof ClientError) {
	//send out the actual message
	res.send(err.code, err.message);
} else {
	//send out a generic message
	res.send(500, 'oops! something went wrong');
	log.error(err);
}

extend-error's People

Contributors

jayyvis avatar mal avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.