collins-error
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -54,6 +54,10 @@ 'use strict'; | ||
static convert (type, error) { | ||
// INFO: given an Error, return a SlackError | ||
let details = { details: error.message }; | ||
if (error) { | ||
if (!(error instanceof Error)) { | ||
throw new CoreError('Invalid:Input', 'invalid function call'); | ||
} | ||
} | ||
let details = { details: error ? error.message : null }; | ||
let convertedError = new CoreError(type, details); | ||
convertedError.stack = error.stack; | ||
convertedError.stack = constructStack(error.stack, convertedError); | ||
return convertedError; | ||
@@ -63,2 +67,15 @@ } | ||
/** | ||
* @summary construct static stack for error | ||
* @param {String} fromStack `.stack` property from error being converted. | ||
* @param {CoreError} constructObj CoreError object to receive stack. | ||
* @return {String} Static stack to be assigned to CoreError object. | ||
*/ | ||
function constructStack (fromStack, constructObj) { | ||
return constructObj.stack.split('\n')[0] + '\n' + | ||
fromStack | ||
.replace(/^[^\(]+?[\n$]/gm, '') | ||
.replace(/^Object.<anonymous>\s*\(/gm, '{anonymous}()@'); | ||
} | ||
module.exports = CoreError; |
{ | ||
"name": "collins-error", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Custom error class used by the collins system", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -30,16 +30,62 @@ 'use strict'; | ||
}); | ||
describe('convert', () => { | ||
let foreignError = new Error('testing'); | ||
it('takes an Error', () => { | ||
Assert.equal(foreignError instanceof Error, true); | ||
Assert.equal(foreignError instanceof CoreError, false); | ||
describe('CoreError#convert', () => { | ||
it('throws if second param is not an Error', () => { | ||
Assert.throws( | ||
() => { | ||
CoreError.convert('TestError', {}); | ||
}, | ||
'invalid function call' | ||
); | ||
Assert.throws( | ||
() => { | ||
CoreError.convert('TestError', 1); | ||
}, | ||
'invalid function call' | ||
); | ||
Assert.throws( | ||
() => { | ||
CoreError.convert('TestError', 'string'); | ||
}, | ||
'invalid function call' | ||
); | ||
Assert.throws( | ||
() => { | ||
CoreError.convert('TestError', () => {}); | ||
}, | ||
'invalid function call' | ||
); | ||
}); | ||
let convertedError = CoreError.convert('TestError', foreignError); | ||
it('should convert Error into CoreError', () => { | ||
let foreignError = new Error('testing'); | ||
let convertedError = CoreError.convert('TestError', foreignError); | ||
Assert.equal(convertedError instanceof CoreError, true); | ||
}); | ||
it('should transfer stack trace to new error object', () => { | ||
Assert.equal(convertedError.stack, foreignError.stack); | ||
it('should convert exceptions into CoreError', () => { | ||
try { | ||
throw new Error('test exception'); | ||
} catch (exception) { | ||
let convertedError = CoreError.convert('TestError', exception); | ||
Assert.equal(convertedError instanceof CoreError, true); | ||
} | ||
}); | ||
it('should contain *shallow* stack from passed exception', () => { | ||
try { | ||
throw new Error('test exception'); | ||
} catch (exception) { | ||
let convertedError = CoreError.convert('TestError', exception); | ||
Assert.deepEqual( | ||
exception.stack.split('\n').slice(1, 5), | ||
convertedError.stack.split('\n').slice(1, 5) | ||
); | ||
} | ||
}); | ||
it('should contain *shallow* stack from passed error', () => { | ||
let foreignError = new Error('testing'); | ||
let convertedError = CoreError.convert('TestError', foreignError); | ||
Assert.deepEqual( | ||
foreignError.stack.split('\n').slice(1, 5), | ||
convertedError.stack.split('\n').slice(1, 5) | ||
); | ||
}); | ||
}); | ||
}); |
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
8289
195