Skip to content

Commit

Permalink
Merge pull request #13 from blacksonic/master
Browse files Browse the repository at this point in the history
feat(error): add constructor data attribute to property instead of extending itself
  • Loading branch information
sonicoder86 committed Mar 10, 2016
2 parents 80c4c1f + 60d33a8 commit ac30b6b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ Usage
}).then(function(response) {
console.log(response);
});

13 changes: 5 additions & 8 deletions requestError.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
'use strict';

var _ = require('lodash');

var SuiteRequestError = function(message, code, data) {
this.message = message;
this.name = 'SuiteRequestError';
this.code = code;
this.data = {
replyText: message
};
this.stack = new Error().stack;

_.extend(this, data);
this.data = data || {};
if (!this.data.replyText) {
this.data.replyText = message;
}
this.stack = new Error(message).stack;
};

SuiteRequestError.prototype = Object.create(Error.prototype);
Expand Down
25 changes: 25 additions & 0 deletions requestError.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

var SuiteRequestError = require('./requestError');

describe('SuiteRequestError', function() {
it('should extend base Error class', function() {
var error = new SuiteRequestError();

expect(error).to.be.an.instanceOf(Error);
});

it('should store constructor parameters', function() {
var error = new SuiteRequestError('Invalid request', 400, { detailedMessage: 'Line too long' });

expect(error.message).to.eql('Invalid request');
expect(error.code).to.eql(400);
expect(error.data).to.eql({ detailedMessage: 'Line too long', replyText: 'Invalid request' });
});

it('should always contain replyText in data', function() {
var error = new SuiteRequestError('Unauthorized');

expect(error.data.replyText).to.eql('Unauthorized');
});
});

0 comments on commit ac30b6b

Please sign in to comment.