Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Properly handle HTTP errors from sendgrid

Closes #46
  • Loading branch information...
commit 6f7adb92b227cb9ebbd369052b6710df9b3fc13c 1 parent 8f8cfc5
@theycallmeswift theycallmeswift authored
View
10 lib/sendgrid.js
@@ -72,9 +72,15 @@ SendGrid.prototype.send = function(email, callback) {
content += chunk;
});
res.on('end', function() {
- var json = JSON.parse(content);
- cb(json.message == 'success', json.errors);
+ try {
+ var json = JSON.parse(content);
+ cb(json.message == 'success', json.errors);
+ } catch (e) {
+ cb(false, "Invalid JSON response from server");
+ }
});
+ }).on('error', function(e) {
+ cb(false, e);
});
// If the email has files, it will be a multipart request.
View
20 npm-shrinkwrap.json
@@ -49,7 +49,25 @@
"version": "0.0.5"
},
"nock": {
- "version": "0.14.4"
+ "version": "0.16.0",
+ "dependencies": {
+ "propagate": {
+ "version": "0.2.2"
+ }
+ }
+ },
+ "sinon": {
+ "version": "1.6.0",
+ "dependencies": {
+ "buster-format": {
+ "version": "0.5.5",
+ "dependencies": {
+ "buster-core": {
+ "version": "0.6.4"
+ }
+ }
+ }
+ }
},
"chai": {
"version": "1.5.0"
View
3  package.json
@@ -22,7 +22,8 @@
"nodemailer": "*",
"mime": "*",
"step": ">= 0.0.5",
- "nock": "~0.14.2"
+ "nock": "~0.16.0",
+ "sinon": "~1.6.0"
},
"devDependencies": {
"mocha": ">= 0.9.0",
View
24 test/lib/sendgrid.test.js
@@ -1,5 +1,6 @@
var SendGrid = require('../../lib/sendgrid')
, querystring = require('querystring')
+ , sinon = require('sinon')
, nock = require('nock');
describe('SendGrid', function () {
@@ -50,6 +51,29 @@ describe('SendGrid', function () {
});
});
+ it('reports http errors to the user', function(done) {
+ var https = require('https')
+ , realRequest = https.request
+ , stub;
+
+ function fakeRequest(options, cb) {
+ var req = realRequest(options, cb);
+ process.nextTick(function() {
+ req.emit('error', 'some http error');
+ });
+ return req;
+ }
+
+ stub = sinon.stub(https, 'request', fakeRequest);
+
+ sendgrid.send({}, function(success, message) {
+ expect(success).to.be.false;
+ expect(message).to.equal("some http error");
+ https.request.restore();
+ done();
+ });
+ });
+
it("returns success if message is 'success'", function(done) {
mock = webApi.reply(200, { message: "success" });
Please sign in to comment.
Something went wrong with that request. Please try again.