From 2e25e44c2caae8db50deb294595e6b1fa042ba14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mustafa=20Dokumac=C4=B1?= Date: Sun, 27 Mar 2016 12:03:19 +0200 Subject: [PATCH 1/3] body might be undefined When body is undefined it throws exceptions. --- lib/sparkpost.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sparkpost.js b/lib/sparkpost.js index 0f2ce9f..0a46acf 100644 --- a/lib/sparkpost.js +++ b/lib/sparkpost.js @@ -97,7 +97,7 @@ SparkPost.prototype.request = function( options, callback ) { } else if(invalidCodeRegex.test(res.statusCode)) { err = new Error(res.statusMessage); err.name = 'SparkPostError'; - err.errors = body.errors; + err.errors = (body || {}).errors; err.statusCode = res.statusCode; return callback(err, res); } else { From 1124308ac6d2ff288e4368fb4b4ee1abc125fa0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mustafa=20Dokumac=C4=B1?= Date: Fri, 1 Apr 2016 14:50:12 +0200 Subject: [PATCH 2/3] Explicit Version --- lib/sparkpost.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/sparkpost.js b/lib/sparkpost.js index 0a46acf..942827c 100644 --- a/lib/sparkpost.js +++ b/lib/sparkpost.js @@ -95,9 +95,10 @@ SparkPost.prototype.request = function( options, callback ) { if(err) { return callback(err, res); } else if(invalidCodeRegex.test(res.statusCode)) { + body = body || {}; err = new Error(res.statusMessage); err.name = 'SparkPostError'; - err.errors = (body || {}).errors; + err.errors = body.errors; err.statusCode = res.statusCode; return callback(err, res); } else { From c57adca39b1af9ed8bc9af4eca5867b03879e0a5 Mon Sep 17 00:00:00 2001 From: "Aydrian J. Howard" Date: Fri, 1 Apr 2016 12:12:23 -0400 Subject: [PATCH 3/3] Added test for null response body --- test/spec/sparkpost.spec.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/test/spec/sparkpost.spec.js b/test/spec/sparkpost.spec.js index 56ba899..3e10f59 100644 --- a/test/spec/sparkpost.spec.js +++ b/test/spec/sparkpost.spec.js @@ -149,6 +149,28 @@ describe('SparkPost Library', function() { }); }); + it('should return an error if statusCode not 2XX and there is no body', function(done) { + // simulate a timeout + nock('https://api.sparkpost.com') + .post('/api/v1/post/test/fail') + .reply(422); + + var options = { + method: 'POST' + , uri: 'post/test/fail' + }; + + client.request(options, function(err, data) { + expect(data).to.be.defined; + expect(err).to.be.defined; + + expect(err.errors).to.be.undefined; + + // finish async test + done(); + }); + }); + it('should use a full URI if provided', function(done) { nock('https://test.sparkpost.com') .get('/test') @@ -194,7 +216,7 @@ describe('SparkPost Library', function() { expect(err).to.be.null; expect(data.statusCode).to.equal(200); expect(data.body).to.equal(TEST_MESSAGE + TEST_MESSAGE); - + // finish async test done(); });