Skip to content

Commit

Permalink
Merge pull request #22 from SparkPost/feature/issue-21
Browse files Browse the repository at this point in the history
Fixes Issue #21
  • Loading branch information
Jessica Martin committed Apr 2, 2015
2 parents b885a29 + be67fb5 commit b597a0c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
9 changes: 9 additions & 0 deletions lib/transmission.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ module.exports = {
request.post(options, function(err, res, body) {
if (err) {
callback(err);
} else if (typeof res === 'undefined' || typeof res.body === 'undefined') {
/*
* If we're in here then something very strange has happened because we didn't
* get a properly formatted response from the API call, so give generic error
* since that's the best we can do in this case
*
* Note that this is the case for both successful responses and error responses
*/
callback(new Error('Unexpected error occurred while trying to send transmission'));
} else if (res.statusCode !== 200) {
callback(res.body.errors);
} else {
Expand Down
2 changes: 1 addition & 1 deletion test/mocks/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ MockRequest.prototype.get = function(options, callbackFunction) {
};

MockRequest.prototype.post = function(options, callbackFunction) {
var response = {error: this.error, response: this.response, body: this.response.body};
var response = {error: this.error, response: this.response, body: this.response.body || undefined};
callbackFunction(response.error, response.response, response.body);
};

Expand Down
12 changes: 12 additions & 0 deletions test/spec/transmissions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,18 @@ describe('Transmissions Library', function() {
MockRequest.restore();
});

it('should return a generic error when we get an ill-formed response from the API', function() {
MockRequest.response = {}; // no body

transmission.send({}, function(err, res) {
expect(res).to.be.undefined;
expect(err).to.match(/Unexpected error occurred while trying to send transmission/);
});

MockRequest.restore();
});


it('should return an error if the status code is anything other than 200', function() {
MockRequest.response.statusCode = 500;
MockRequest.response.body.errors[0] = 'first error';
Expand Down

0 comments on commit b597a0c

Please sign in to comment.