small error improvements

no issue

- in Ignition we have added keeping the original stack, i copied it over
- i would like to use Ignition in Ghost asap to avoid having inconsistencies
- added support for options.err is a string
- extend tests
This commit is contained in:
kirrg001 2016-11-09 09:13:50 +01:00
parent e3d6e02aed
commit d7c8da7ee8
2 changed files with 25 additions and 0 deletions

View File

@ -42,11 +42,28 @@ function GhostError(options) {
// error to inherit from, override!
// nested objects are getting copied over in one piece (can be changed, but not needed right now)
if (options.err) {
// it can happen that third party libs return errors as strings, yes really
// we are creating an error stack from this line, but we need to ensure not loosing the original error message
if (_.isString(options.err)) {
options.err = new Error(options.err);
}
Object.getOwnPropertyNames(options.err).forEach(function (property) {
// original message is part of the stack, no need to pick it
if (['errorType', 'name', 'statusCode'].indexOf(property) !== -1) {
return;
}
if (property === 'message' && !self[property]) {
self[property] = options.err[property];
return;
}
if (property === 'stack') {
self[property] += '\n\n' + options.err[property];
return;
}
self[property] = options.err[property] || self[property];
});
}

View File

@ -67,5 +67,13 @@ describe('Errors', function () {
ghostError.message.should.eql(someError.message);
ghostError.context.should.eql('context');
});
it('error is string', function () {
var ghostError = new errors.GhostError({
err: 'string'
});
ghostError.message.should.eql('string');
});
});
});