From b253ba43d53dcdb5b15729d0041e9392b4c28db8 Mon Sep 17 00:00:00 2001 From: blacksonic Date: Fri, 11 Mar 2016 11:12:24 +0100 Subject: [PATCH 1/5] fix(error): reponse body is readonly, not possible to reassign replyText --- requestError.js | 3 --- requestError.spec.js | 4 ++-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/requestError.js b/requestError.js index 10dc9bf..ff4b653 100644 --- a/requestError.js +++ b/requestError.js @@ -5,9 +5,6 @@ var SuiteRequestError = function(message, code, data) { this.name = 'SuiteRequestError'; this.code = code; this.data = data || {}; - if (!this.data.replyText) { - this.data.replyText = message; - } this.stack = new Error(message).stack; }; diff --git a/requestError.spec.js b/requestError.spec.js index 5e451c7..b28fdf2 100644 --- a/requestError.spec.js +++ b/requestError.spec.js @@ -14,12 +14,12 @@ describe('SuiteRequestError', function() { 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({ detailedMessage: 'Line too long' }); }); it('should always contain replyText in data', function() { var error = new SuiteRequestError('Unauthorized'); - expect(error.data.replyText).to.eql('Unauthorized'); + expect(error.data).to.eql({ }); }); }); From 1676b99b43b79ff12834c6336b1ec3f94e9a8ef7 Mon Sep 17 00:00:00 2001 From: blacksonic Date: Fri, 11 Mar 2016 11:15:59 +0100 Subject: [PATCH 2/5] fix(error): fix test name --- requestError.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requestError.spec.js b/requestError.spec.js index b28fdf2..7bd68e1 100644 --- a/requestError.spec.js +++ b/requestError.spec.js @@ -17,7 +17,7 @@ describe('SuiteRequestError', function() { expect(error.data).to.eql({ detailedMessage: 'Line too long' }); }); - it('should always contain replyText in data', function() { + it('should always contain data on error', function() { var error = new SuiteRequestError('Unauthorized'); expect(error.data).to.eql({ }); From 438334d13767929b95db2b3379adab192c7764f8 Mon Sep 17 00:00:00 2001 From: blacksonic Date: Fri, 11 Mar 2016 13:07:32 +0100 Subject: [PATCH 3/5] fix(error): revert to old behavior, make it more readable BREAKING CHANGE: revert behavior --- requestError.js | 11 +++++++++-- requestError.spec.js | 28 +++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/requestError.js b/requestError.js index ff4b653..fb934da 100644 --- a/requestError.js +++ b/requestError.js @@ -1,10 +1,17 @@ 'use strict'; -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 (response) { + this.data = response.data || response; + } else { + this.data = { + replyText: message + }; + } + this.stack = new Error(message).stack; }; diff --git a/requestError.spec.js b/requestError.spec.js index 7bd68e1..fc2538c 100644 --- a/requestError.spec.js +++ b/requestError.spec.js @@ -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' }); + expect(error.data).to.eql({ + replyText: 'Too long', + detailedMessage: 'Line too long' + }); + }); + + 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).to.eql({ }); + expect(error.data).to.eql({ replyText: 'Unauthorized' }); }); }); From 6e81e10f99d632e24717428a0aca9e96d5bf98da Mon Sep 17 00:00:00 2001 From: blacksonic Date: Fri, 11 Mar 2016 13:17:17 +0100 Subject: [PATCH 4/5] fix(error): use cloned object --- requestError.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requestError.js b/requestError.js index fb934da..947fde1 100644 --- a/requestError.js +++ b/requestError.js @@ -1,11 +1,12 @@ 'use strict'; +var _ = require('lodash'); var SuiteRequestError = function(message, code, response) { this.message = message; this.name = 'SuiteRequestError'; this.code = code; if (response) { - this.data = response.data || response; + this.data = _.cloneDeep(response.data|| response); } else { this.data = { replyText: message From 7f374eb9bb0bcfb237ec7e953771592674b4e3bb Mon Sep 17 00:00:00 2001 From: blacksonic Date: Fri, 11 Mar 2016 13:20:17 +0100 Subject: [PATCH 5/5] fix(error): fix style --- requestError.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requestError.js b/requestError.js index 947fde1..7f6d370 100644 --- a/requestError.js +++ b/requestError.js @@ -6,7 +6,7 @@ var SuiteRequestError = function(message, code, response) { this.name = 'SuiteRequestError'; this.code = code; if (response) { - this.data = _.cloneDeep(response.data|| response); + this.data = _.cloneDeep(response.data || response); } else { this.data = { replyText: message