error.js
v1.2.1
Published
Custom errors in javascript (browser and server support)
Downloads
495
Maintainers
Readme
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);