0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Merge pull request #7698 from kirrg001/1.0.0-dev/error-inheritance-improvement

  small error improvements
This commit is contained in:
Hannah Wolfe 2016-11-14 12:27:15 +00:00 committed by GitHub
commit f69ec600c0
2 changed files with 25 additions and 0 deletions

View file

@ -42,11 +42,28 @@ function GhostError(options) {
// error to inherit from, override! // error to inherit from, override!
// nested objects are getting copied over in one piece (can be changed, but not needed right now) // nested objects are getting copied over in one piece (can be changed, but not needed right now)
if (options.err) { 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) { 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) { if (['errorType', 'name', 'statusCode'].indexOf(property) !== -1) {
return; 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]; self[property] = options.err[property] || self[property];
}); });
} }

View file

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