error.js

1.2.1 • Public • Published

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);

Package Sidebar

Install

npm i error.js

Weekly Downloads

68

Version

1.2.1

License

MIT

Last publish

Collaborators

  • krossnine