From d7c8da7ee866e9275799b3fef9742e5539d9ec81 Mon Sep 17 00:00:00 2001 From: kirrg001 Date: Wed, 9 Nov 2016 09:13:50 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=20small=20error=20improvements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- core/server/errors.js | 17 +++++++++++++++++ core/test/unit/errors_spec.js | 8 ++++++++ 2 files changed, 25 insertions(+) diff --git a/core/server/errors.js b/core/server/errors.js index e457856949..be3d037ad9 100644 --- a/core/server/errors.js +++ b/core/server/errors.js @@ -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]; }); } diff --git a/core/test/unit/errors_spec.js b/core/test/unit/errors_spec.js index 4cbccc9333..e245b5648e 100644 --- a/core/test/unit/errors_spec.js +++ b/core/test/unit/errors_spec.js @@ -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'); + }); }); });