1.2.1 • Published 8 years ago

error.js v1.2.1

Weekly downloads
291
License
MIT
Repository
github
Last release
8 years ago

Build Status Dependency Status devDependency Status Build Status

Create custom errors in javascript

var MyCustomError = CustomError.create("MyCustomError");

throw new MyCustomError("Ooops");

Installation

$ npm install --save error.js

Quick start

/* Require the dependency */
var CustomError = require("error.js");

/* Create your custom error once */
var MyCustomError = CustomError.create("MyCustomError");

/* Throw your custom error */
throw new MyCustomError("oops");

Usage

Create a custom error :

var MyCustomName = CustomError.create("MyCustomName");

Create a custom error with additional properties :

var NotFoundError = CustomError.create({
	name : "NotFound",
	message : "Content not found",
	statusCode : 404
});

Throw a custom error :

throw new MyCustomError("oops");

Throw a custom error with additional properties :

throw new NotFoundError({
	message : "Could not find requested user",
	userId : "123-456-789"
});

Check if an Error is a custom error :

var MyCustomError = CustomError.create("MyCustomError");
var myCustomError = new MyCustomError("oops");

/* Will return true */
CustomError.isCustom(myCustomError);
var e = new Error("oops");

/* Will return false */
CustomError.isCustom(e);