Permalink
Browse files

use Error for handling errors so stack trace can be read

  • Loading branch information...
1 parent 5bc846c commit 13916d11f2d6c053ab2d7660775de8cc6c0077e2 eleith committed Aug 3, 2012
Showing with 32 additions and 27 deletions.
  1. +2 −2 smtp/client.js
  2. +19 −14 smtp/error.js
  3. +4 −4 smtp/response.js
  4. +7 −7 smtp/smtp.js
View
@@ -99,11 +99,11 @@ Client.prototype =
self._poll();
}
else
- callback({code:-1, message:why}, msg);
+ callback(new Error(why), msg);
});
}
else
- callback({code:-1, message:"message is not a valid Message instance"}, msg);
+ callback(new Error("message is not a valid Message instance"), msg);
},
_sendsmtp: function(stack, next)
View
@@ -1,16 +1,21 @@
-var SMTPError =
+module.exports = function(message, code, error, smtp)
{
- COULDNOTCONNECT: 1,
- BADRESPONSE: 2,
- AUTHFAILED: 3,
- TIMEDOUT: 4,
- ERROR: 5,
- NOCONNECTION: 6,
- AUTHNOTSUPPORTED: 7,
- CONNECTIONCLOSED: 8,
- CONNECTIONENDED: 9,
- CONNECTIONAUTH: 10
-}
+ var err = new Error(message);
+ err.code = code;
+ if(error)
+ err.previous = error;
+ err.smtp = smtp;
-for(var each in SMTPError)
- exports[each] = SMTPError[each];
+ return err;
+};
+
+module.exports.COULDNOTCONNECT = 1;
+module.exports.BADRESPONSE = 2;
+module.exports.AUTHFAILED = 3;
+module.exports.TIMEDOUT = 4;
+module.exports.ERROR = 5;
+module.exports.NOCONNECTION = 6;
+module.exports.AUTHNOTSUPPORTED = 7;
+module.exports.CONNECTIONCLOSED = 8;
+module.exports.CONNECTIONENDED = 9;
+module.exports.CONNECTIONAUTH = 10;
View
@@ -19,12 +19,12 @@ var SMTPResponse = function(stream, timeout, onerror)
error = function(err)
{
- stream.emit('response', {code:SMTPError.ERROR, message:"connection encountered an error", error:err});
+ stream.emit('response', SMTPError('connection encountered an error', SMTPError.ERROR, err));
},
timedout = function(err)
{
- stream.emit('response', {code:SMTPError.TIMEDOUT, message:"timedout while connecting to smtp server", error:err});
+ stream.emit('response', SMTPError('timedout while connecting to smtp server', SMTPError.TIMEDOUT, err));
},
watch = function(data)
@@ -39,12 +39,12 @@ var SMTPResponse = function(stream, timeout, onerror)
close = function(err)
{
- stream.emit('response', {code:SMTPError.CONNECTIONCLOSED, message:"connection has closed", error:err});
+ stream.emit('response', SMTPError('connection has closed', SMTPError.CONNECTIONCLOSED, err));
},
end = function(err)
{
- stream.emit('response', {code:SMTPError.CONNECTIONENDED, message:"connection has ended", error:err});
+ stream.emit('response', SMTPError('connection has ended', SMTPError.CONNECTIONENDED, err));
};
this.stop = function(err)
View
@@ -121,7 +121,7 @@ SMTP.prototype =
if(typeof(self.ssl) != 'boolean' && !self.sock.authorized)
{
self.close(true);
- caller(callback, {code:SMTPError.CONNECTIONAUTH, message:"could not establish an ssl connection", error:err});
+ caller(callback, SMTPError('could not establish an ssl connection', SMTPError.CONNECTIONAUTH, err));
}
else
self._secure = true;
@@ -130,7 +130,7 @@ SMTP.prototype =
else
{
self.close(true);
- caller(callback, {code:SMTPError.COULDNOTCONNECT, error:err});
+ caller(callback, SMTPError("could not connect", SMTPError.COULDNOTCONNECT, err));
}
};
@@ -152,7 +152,7 @@ SMTP.prototype =
{
log("response (data): " + msg.data);
self.quit();
- caller(callback, {code:SMTPError.BADRESPONSE, message:"bad response on connection", smtp:msg.data, error:err});
+ caller(callback, SMTPError("bad response on connection", SMTPError.BADRESPONSE, err, msg.data));
}
};
@@ -200,7 +200,7 @@ SMTP.prototype =
else
{
self.close(true);
- caller(callback, {code:SMTPError.NOCONNECTION, message:"no connection has been established"});
+ caller(callback, SMTPError('no connection has been established', SMTPError.NOCONNECTION));
}
},
@@ -220,7 +220,7 @@ SMTP.prototype =
caller(callback, err, msg.data, msg.message);
else
- caller(callback, {code:SMTPError.BADRESPONSE, message:"bad response on command '" + cmd.split(' ')[0] + "'", smtp:msg.data});
+ caller(callback, SMTPError("bad response on command '" + cmd.split(' ')[0] + "'", SMTPError.BADRESPONSE, null, msg.data));
}
};
@@ -480,7 +480,7 @@ SMTP.prototype =
var failed = function(err, data)
{
self.loggedin = false;
- caller(callback, {code:SMTPError.AUTHFAILED, message:"authorization failed", smtp:data, error:err.error});
+ caller(callback, SMTPError('authorization.failed', SMTPError.AUTHFAILED, err, data));
};
var response = function(err, data)
@@ -535,7 +535,7 @@ SMTP.prototype =
self.command("AUTH " + AUTH_METHODS.PLAIN + " " + encode_plain(login.user(), login.password()), response, [235, 503]);
else if(!method)
- caller(callback, {code:SMTPError.AUTHNOTSUPPORTED, message:"no form of authorization supported", smtp:data});
+ caller(callback, SMTPError('no form of authorization supported', SMTPError.AUTHNOTSUPPORTED, null, data));
};
self.ehlo_or_helo_if_needed(initiate, domain);

2 comments on commit 13916d1

@defunctzombie
Contributor

awesome!

@tedeh
Contributor
tedeh replied Aug 6, 2012

Thanks!

Please sign in to comment.