Comparing version 3.0.1 to 4.0.0
@@ -76,3 +76,5 @@ 'use strict'; | ||
value: function toString() { | ||
return (this.wrappedErrors.length ? this.wrappedErrors[this.wrappedErrors.length - 1].stack + os.EOL : '') + this.constructor.name + ': ' + this.code + ' (' + this.params.join(', ') + ')'; | ||
return (this.wrappedErrors.length ? | ||
// eslint-disable-next-line | ||
this.wrappedErrors[this.wrappedErrors.length - 1].stack + os.EOL : '') + this.constructor.name + ': ' + this.code + ' (' + this.params.join(', ') + ')'; | ||
} | ||
@@ -89,3 +91,3 @@ }]); | ||
var yError = null; | ||
var wrappedErrorIsACode = /^([A-Z0-9_]+)$/.test(err.message); | ||
var wrappedErrorIsACode = _looksLikeAYErrorCode(err.message); | ||
var wrappedErrors = (err.wrappedErrors || []).concat(err); | ||
@@ -113,3 +115,3 @@ | ||
YError.cast = function yerrorCast(err) { | ||
if (err instanceof YError) { | ||
if (_looksLikeAYError(err)) { | ||
return err; | ||
@@ -126,3 +128,3 @@ } | ||
YError.bump = function yerrorBump(err) { | ||
if (err instanceof YError) { | ||
if (_looksLikeAYError(err)) { | ||
return YError.wrap.apply(YError, [err, err.code].concat(err.params)); | ||
@@ -138,2 +140,13 @@ } | ||
// In order to keep compatibility through major versions | ||
// we have to make kind of an cross major version instanceof | ||
function _looksLikeAYError(err) { | ||
return err instanceof YError || err.constructor && err.constructor.name && err.constructor.name.endsWith('Error') && 'string' === typeof err.code && _looksLikeAYErrorCode(err.code) && err.params && err.params instanceof Array; | ||
} | ||
function _looksLikeAYErrorCode(str) { | ||
return (/^([A-Z0-9_]+)$/.test(str) | ||
); | ||
} | ||
module.exports = YError; |
@@ -145,2 +145,21 @@ /* eslint max-nested-callbacks:[0], no-magic-numbers:[0] */ | ||
it('Should work with YError like errors', function () { | ||
var baseErr = new Error('E_A_NEW_ERROR'); | ||
baseErr.code = 'E_A_NEW_ERROR'; | ||
baseErr.params = ['baseParam1', 'baseParam2']; | ||
var err = YError.bump(baseErr); | ||
assert.equal(err.code, 'E_A_NEW_ERROR'); | ||
assert.equal(err.wrappedErrors.length, 1); | ||
assert.deepEqual(err.params, ['baseParam1', 'baseParam2']); | ||
if (Error.captureStackTrace) { | ||
assert(-1 !== err.stack.indexOf('Error: E_A_NEW_ERROR'), 'Stack contains original error.'); | ||
assert(-1 !== err.stack.indexOf('YError: E_A_NEW_ERROR (baseParam1, baseParam2)'), 'Stack contains bumped error.'); | ||
} | ||
assert.equal(err.name, err.toString()); | ||
}); | ||
it('Should work with Y errors and a message', function () { | ||
@@ -147,0 +166,0 @@ var err = YError.bump(new YError('E_ERROR', 'arg1.1', 'arg1.2'), 'E_ERROR_2', 'arg2.1', 'arg2.2'); |
{ | ||
"name": "yerror", | ||
"version": "3.0.1", | ||
"version": "4.0.0", | ||
"description": "It helps to know why you got an error.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -48,3 +48,3 @@ 'use strict'; | ||
let yError = null; | ||
const wrappedErrorIsACode = (/^([A-Z0-9_]+)$/).test(err.message); | ||
const wrappedErrorIsACode = _looksLikeAYErrorCode(err.message); | ||
const wrappedErrors = (err.wrappedErrors || []).concat(err); | ||
@@ -67,3 +67,3 @@ | ||
YError.cast = function yerrorCast(err, ...params) { | ||
if(err instanceof YError) { | ||
if(_looksLikeAYError(err)) { | ||
return err; | ||
@@ -75,3 +75,3 @@ } | ||
YError.bump = function yerrorBump(err, ...params) { | ||
if(err instanceof YError) { | ||
if(_looksLikeAYError(err)) { | ||
return YError.wrap.apply(YError, [err, err.code].concat(err.params)); | ||
@@ -82,2 +82,18 @@ } | ||
// In order to keep compatibility through major versions | ||
// we have to make kind of an cross major version instanceof | ||
function _looksLikeAYError(err) { | ||
return err instanceof YError || ( | ||
err.constructor && | ||
err.constructor.name && | ||
err.constructor.name.endsWith('Error') && | ||
'string' === typeof err.code && _looksLikeAYErrorCode(err.code) && | ||
err.params && err.params instanceof Array | ||
); | ||
} | ||
function _looksLikeAYErrorCode(str) { | ||
return (/^([A-Z0-9_]+)$/).test(str); | ||
} | ||
module.exports = YError; |
@@ -199,2 +199,27 @@ /* eslint max-nested-callbacks:[0], no-magic-numbers:[0] */ | ||
it('Should work with YError like errors', () => { | ||
var baseErr = new Error('E_A_NEW_ERROR'); | ||
baseErr.code = 'E_A_NEW_ERROR'; | ||
baseErr.params = ['baseParam1', 'baseParam2']; | ||
var err = YError.bump(baseErr); | ||
assert.equal(err.code, 'E_A_NEW_ERROR'); | ||
assert.equal(err.wrappedErrors.length, 1); | ||
assert.deepEqual(err.params, ['baseParam1', 'baseParam2']); | ||
if(Error.captureStackTrace) { | ||
assert( | ||
-1 !== err.stack.indexOf('Error: E_A_NEW_ERROR'), | ||
'Stack contains original error.' | ||
); | ||
assert( | ||
-1 !== err.stack.indexOf('YError: E_A_NEW_ERROR (baseParam1, baseParam2)'), | ||
'Stack contains bumped error.' | ||
); | ||
} | ||
assert.equal(err.name, err.toString()); | ||
}); | ||
it('Should work with Y errors and a message', () => { | ||
@@ -201,0 +226,0 @@ var err = YError.bump( |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
224110
15
667