punch-error
Advanced tools
Comparing version 1.0.4 to 1.0.5
29
index.js
@@ -19,2 +19,4 @@ 'use strict'; | ||
this.status = config.status || 500; | ||
this.history = []; | ||
this.passed = []; | ||
if (typeof Error.captureStackTrace === 'function') { | ||
@@ -33,6 +35,11 @@ Error.captureStackTrace(this, this.constructor); | ||
/** | ||
* Return self as rejected promise. | ||
* Return self as rejected promise. If provided an error, the error will be | ||
* screened. | ||
*/ | ||
reject() { | ||
return Promise.reject(this); | ||
reject(err, strict) { | ||
const constructor = this.constructor; | ||
let newErr; | ||
if (err) newErr = constructor.screen(err, this, strict); | ||
else newErr = this; | ||
return Promise.reject(newErr); | ||
} | ||
@@ -44,3 +51,3 @@ | ||
*/ | ||
static is(test) { | ||
static is(test, strict) { | ||
if (!test || !(test instanceof Error)) return false; | ||
@@ -52,2 +59,3 @@ let current = test.constructor; | ||
if (current.name === this.name) return true; | ||
if (!strict && (current.name === ApplicationError.name)) return true; | ||
current = Object.getPrototypeOf(current); | ||
@@ -72,7 +80,10 @@ } | ||
*/ | ||
static screen(err, otherwise) { | ||
if (this.is(err)) return err; | ||
static screen(err, otherwise, strict) { | ||
const newError = isPunchError(otherwise) ? otherwise : (new this(otherwise)); | ||
if (this.is(err, strict)) { | ||
err.passed.push(newError); | ||
return err; | ||
} | ||
if (err) { | ||
@@ -105,4 +116,4 @@ newError.history = (err.history && err.history.slice()) || []; | ||
*/ | ||
static reject(err, otherwise) { | ||
return this.screen(err, otherwise).reject(); | ||
static reject(err, otherwise, strict) { | ||
return this.screen(err, otherwise, strict).reject(); | ||
} | ||
@@ -109,0 +120,0 @@ |
{ | ||
"name": "punch-error", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "Tomas Savigliano", |
9086
294