Skip to content

Commit

Permalink
馃帹 optimise error to inherit from in GhostError prototype (#7529)
Browse files Browse the repository at this point in the history
refs #7116
- add errors_spec
- inherit all given attribute values
  • Loading branch information
kirrg001 authored and ErisDS committed Oct 10, 2016
1 parent 2e9aa8c commit f570aae
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
11 changes: 9 additions & 2 deletions core/server/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var _ = require('lodash'),

function GhostError(options) {
options = options || {};
var self = this;

if (_.isString(options)) {
throw new Error('Please instantiate Errors with the option pattern. e.g. new errors.GhostError({message: ...})');
Expand Down Expand Up @@ -37,9 +38,15 @@ function GhostError(options) {
this.hideStack = options.hideStack;

// 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) {
this.message = options.err.message;
this.stack = options.err.stack;
Object.getOwnPropertyNames(options.err).forEach(function (property) {
if (['errorType', 'name', 'statusCode'].indexOf(property) !== -1) {
return;
}

self[property] = options.err[property] || self[property];
});
}
}

Expand Down
66 changes: 66 additions & 0 deletions core/test/unit/errors_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
var errors = require('../../server/errors'),
should = require('should');

should.equal(true, true);

describe('Errors', function () {
describe('Inherite from other error', function () {
it('default', function () {
var someError = new Error(), ghostError;

someError.message = 'test';
someError.context = 'test';
someError.help = 'test';

ghostError = new errors.GhostError({err: someError});
ghostError.message.should.eql(someError.message);
ghostError.context.should.eql(someError.context);
ghostError.help.should.eql(someError.help);
});

it('has nested object', function () {
var someError = new Error(), ghostError;

someError.message = 'test';
someError.obj = {
a: 'b'
};

ghostError = new errors.GhostError({
err: someError
});

ghostError.message.should.eql(someError.message);
ghostError.obj.should.eql(someError.obj);
});

it('with custom attribute', function () {
var someError = new Error(), ghostError;

someError.message = 'test';
someError.context = 'test';

ghostError = new errors.GhostError({
err: someError,
context: 'context'
});

ghostError.message.should.eql(someError.message);
ghostError.context.should.eql('test');
});

it('with custom attribute', function () {
var someError = new Error(), ghostError;

someError.message = 'test';

ghostError = new errors.GhostError({
err: someError,
context: 'context'
});

ghostError.message.should.eql(someError.message);
ghostError.context.should.eql('context');
});
});
});

0 comments on commit f570aae

Please sign in to comment.