From 33ca0dde50cdf17e70e78b8635a85bd489e4c93b Mon Sep 17 00:00:00 2001 From: blacksonic Date: Thu, 10 Mar 2016 11:43:29 +0100 Subject: [PATCH 1/4] feat(error): add constructor data attribute to property instead of extending itself --- requestError.js | 8 +------- requestError.spec.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 requestError.spec.js diff --git a/requestError.js b/requestError.js index e252e3c..9411bf2 100644 --- a/requestError.js +++ b/requestError.js @@ -1,17 +1,11 @@ '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.data = data; this.stack = new Error().stack; - - _.extend(this, data); }; SuiteRequestError.prototype = Object.create(Error.prototype); diff --git a/requestError.spec.js b/requestError.spec.js new file mode 100644 index 0000000..aedbc94 --- /dev/null +++ b/requestError.spec.js @@ -0,0 +1,19 @@ +'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' }); + }); +}); From 299db46cff6c28a3129a500628149124a84936f6 Mon Sep 17 00:00:00 2001 From: blacksonic Date: Thu, 10 Mar 2016 13:00:42 +0100 Subject: [PATCH 2/4] feat(error): add message to stack --- requestError.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requestError.js b/requestError.js index 9411bf2..f196fa8 100644 --- a/requestError.js +++ b/requestError.js @@ -5,7 +5,7 @@ var SuiteRequestError = function(message, code, data) { this.name = 'SuiteRequestError'; this.code = code; this.data = data; - this.stack = new Error().stack; + this.stack = new Error(message).stack; }; SuiteRequestError.prototype = Object.create(Error.prototype); From dbffcedc703431b484f79b5f2c2dd6537e697506 Mon Sep 17 00:00:00 2001 From: blacksonic Date: Thu, 10 Mar 2016 13:27:08 +0100 Subject: [PATCH 3/4] feat(error): add replyText to data always --- requestError.js | 5 ++++- requestError.spec.js | 8 +++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/requestError.js b/requestError.js index f196fa8..10dc9bf 100644 --- a/requestError.js +++ b/requestError.js @@ -4,7 +4,10 @@ var SuiteRequestError = function(message, code, data) { this.message = message; this.name = 'SuiteRequestError'; this.code = code; - this.data = data; + 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 aedbc94..5e451c7 100644 --- a/requestError.spec.js +++ b/requestError.spec.js @@ -14,6 +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' }); + 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'); }); }); From 60d33a800cf3c9e79c81bace39166cc574897483 Mon Sep 17 00:00:00 2001 From: blacksonic Date: Thu, 10 Mar 2016 13:41:14 +0100 Subject: [PATCH 4/4] feat(error): dummy readme BREAKING CHANGE: error wont be extended with response data --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index cf30994..7c7b847 100644 --- a/README.md +++ b/README.md @@ -18,3 +18,4 @@ Usage }).then(function(response) { console.log(response); }); +