diff options
author | Swift <theycallmeswift@gmail.com> | 2013-03-25 14:58:42 +0100 |
---|---|---|
committer | Swift <theycallmeswift@gmail.com> | 2013-03-25 15:07:07 +0100 |
commit | 6f7adb92b227cb9ebbd369052b6710df9b3fc13c (patch) | |
tree | 3f83fd0ccc47901b4702d731077f9dbee2d255fa /lib | |
parent | 8f8cfc51e37101c7b4a9f8591d09ac756c9b8e90 (diff) | |
download | sendgrid-nodejs-6f7adb92b227cb9ebbd369052b6710df9b3fc13c.zip sendgrid-nodejs-6f7adb92b227cb9ebbd369052b6710df9b3fc13c.tar.gz sendgrid-nodejs-6f7adb92b227cb9ebbd369052b6710df9b3fc13c.tar.bz2 |
Properly handle HTTP errors from sendgrid
Closes #46
Diffstat (limited to 'lib')
-rw-r--r-- | lib/sendgrid.js | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/lib/sendgrid.js b/lib/sendgrid.js index 12c8b85..b01d4dd 100644 --- a/lib/sendgrid.js +++ b/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. |