diff options
author | scottmotte <scott@scottmotte.com> | 2013-07-22 17:10:37 -0700 |
---|---|---|
committer | scottmotte <scott@scottmotte.com> | 2013-07-23 10:07:56 -0700 |
commit | 0b49f89e7a40cc48248c3cfb16b15fbf3526241d (patch) | |
tree | ce7028b004da9f7a8fe69d62c6be4d9d17bcd12b /lib | |
parent | a95cbfaee276114c2e7985dcb8f2d02d6111b465 (diff) | |
download | sendgrid-nodejs-0b49f89e7a40cc48248c3cfb16b15fbf3526241d.zip sendgrid-nodejs-0b49f89e7a40cc48248c3cfb16b15fbf3526241d.tar.gz sendgrid-nodejs-0b49f89e7a40cc48248c3cfb16b15fbf3526241d.tar.bz2 |
Swaps out the bits of code to be more node like and sends errors first, then response if success
Diffstat (limited to 'lib')
-rw-r--r-- | lib/sendgrid.js | 39 |
1 files changed, 10 insertions, 29 deletions
diff --git a/lib/sendgrid.js b/lib/sendgrid.js index a78c702..3320b36 100644 --- a/lib/sendgrid.js +++ b/lib/sendgrid.js @@ -55,34 +55,16 @@ SendGrid.prototype.web = function(email, callback) { method : 'POST', uri : "https://sendgrid.com/api/mail.send.json" }, function(err, resp, body) { - try { - var json = JSON.parse(body); - } catch (err) { - return cb(err); - } + if(err) return cb(err, null); + var json = JSON.parse(body); - var err = undefined; if (json.message !== 'success') { - var msg = 'sendgrid error'; - if (json.errors) { - msg = json.errors.shift(); - } - - return cb(new Error(msg)); + var error = 'sendgrid error'; + if (json.errors) { error = json.errors.shift(); } + return cb(error, null); } - cb(err); - - //if (err) { - // return cb(false, err); - //} else { - // try { - // var json = JSON.parse(body); - // return cb(json.message == 'success', json.errors); - // } catch (err) { - // cb(false, "Invalid JSON response from server"); - // } - //} + return cb(null, json); }); var form = email.toWebFormat(); @@ -132,12 +114,11 @@ SendGrid.prototype.smtp = function(email, callback) { }); function send_smtp() { - smtpTransport.sendMail(email.toSmtpFormat(), function(error, response) { + smtpTransport.sendMail(email.toSmtpFormat(), function(err, response) { smtpTransport.close(); - if(error) { - return cb(false, error.data); - } - return cb(true, response); + if(err) { return cb(error.data, null);} + + return cb(null, {'message': 'success'}); }); } |