Permalink
Browse files

all errors go to req object

  • Loading branch information...
1 parent 105c35b commit 4125822bedfb84b052ead699946296ce3cfa691c @ry ry committed Jan 20, 2011
Showing with 28 additions and 6 deletions.
  1. +28 −6 lib/http.js
View
34 lib/http.js
@@ -810,6 +810,7 @@ function connectionListener(socket) {
var parser = parsers.alloc();
parser.reinitialize('request');
parser.socket = socket;
+ parser.incoming = null;
socket.addListener('error', function(e) {
self.emit('clientError', e);
@@ -953,6 +954,20 @@ Agent.prototype._establishNewConnection = function() {
var parser = parsers.alloc();
parser.reinitialize('response');
parser.socket = socket;
+ parser.incoming = null;
+
+ socket.on('error', function(err) {
+ debug("AGENT SOCKET ERROR: " + err.message);
+ if (socket._httpMessage) {
+ socket._httpMessage.emit('error', err);
+ } else if (self.queue.length) {
+ var req = self.queue.shift();
+ if (req) req.emit('error', err);
+ } else {
+ // No requests on queue? Where is the request
+ assert(0);
+ }
+ });
socket.ondata = function(d, start, end) {
var ret = parser.execute(d, start, end - start);
@@ -998,6 +1013,8 @@ Agent.prototype._establishNewConnection = function() {
var req = socket._httpMessage;
assert(req);
+ req.res = res;
+
// Responses to HEAD requests are AWFUL. Ask Ryan.
// A major oversight in HTTP. Hence this nastiness.
var isHeadResponse = req.method == 'HEAD';
@@ -1100,11 +1117,7 @@ exports.request = function(options, cb) {
var agent = getAgent(options.host, options.port);
var req = agent.appendMessage(options);
- if (cb) {
- req.once('response', function (res) {
- cb(null, res);
- });
- }
+ if (cb) req.once('response', cb);
return req;
};
@@ -1123,6 +1136,7 @@ function Client(port, host) {
this.port = port;
this.host = host;
}
+util.inherits(Client, EventEmitter);
Client.prototype.request = function(method, path, headers) {
@@ -1141,7 +1155,15 @@ Client.prototype.request = function(method, path, headers) {
host: this.host
};
- return exports.request(options);
+ var self = this;
+ var req = exports.request(options);
+
+ // proxy error events from req to Client
+ req.on('error', function(err) {
+ self.emit('error', err);
+ });
+
+ return req;
};

0 comments on commit 4125822

Please sign in to comment.