Skip to content

Commit

Permalink
Merge pull request #14 from blacksonic/master
Browse files Browse the repository at this point in the history
fix(error): reponse body is readonly, not possible to reassign replyText
  • Loading branch information
sonicoder86 committed Mar 11, 2016
2 parents ac30b6b + 7f374eb commit 9d4c026
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
13 changes: 9 additions & 4 deletions requestError.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
'use strict';
var _ = require('lodash');

var SuiteRequestError = function(message, code, data) {
var SuiteRequestError = function(message, code, response) {
this.message = message;
this.name = 'SuiteRequestError';
this.code = code;
this.data = data || {};
if (!this.data.replyText) {
this.data.replyText = message;
if (response) {
this.data = _.cloneDeep(response.data || response);
} else {
this.data = {
replyText: message
};
}

this.stack = new Error(message).stack;
};

Expand Down
30 changes: 26 additions & 4 deletions requestError.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,38 @@ describe('SuiteRequestError', function() {
});

it('should store constructor parameters', function() {
var error = new SuiteRequestError('Invalid request', 400, { detailedMessage: 'Line too long' });
var error = new SuiteRequestError('Invalid request', 400, {
data: {
replyText: 'Too long',
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' });
expect(error.data).to.eql({
replyText: 'Too long',
detailedMessage: 'Line too long'
});
});

it('should always contain replyText in data', function() {
it('should store response as is when no data attribute present', function() {
var error = new SuiteRequestError('Invalid request', 400, {
replyText: 'Too long',
detailedMessage: 'Line too long'
});

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

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

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

0 comments on commit 9d4c026

Please sign in to comment.