Skip to content
This repository
Browse code

all errors go to req object

  • Loading branch information...
commit 4125822bedfb84b052ead699946296ce3cfa691c 1 parent 105c35b
ry ry authored

Showing 1 changed file with 28 additions and 6 deletions. Show diff stats Hide diff stats

  1. +28 6 lib/http.js
34 lib/http.js
@@ -810,6 +810,7 @@ function connectionListener(socket) {
810 810 var parser = parsers.alloc();
811 811 parser.reinitialize('request');
812 812 parser.socket = socket;
  813 + parser.incoming = null;
813 814
814 815 socket.addListener('error', function(e) {
815 816 self.emit('clientError', e);
@@ -953,6 +954,20 @@ Agent.prototype._establishNewConnection = function() {
953 954 var parser = parsers.alloc();
954 955 parser.reinitialize('response');
955 956 parser.socket = socket;
  957 + parser.incoming = null;
  958 +
  959 + socket.on('error', function(err) {
  960 + debug("AGENT SOCKET ERROR: " + err.message);
  961 + if (socket._httpMessage) {
  962 + socket._httpMessage.emit('error', err);
  963 + } else if (self.queue.length) {
  964 + var req = self.queue.shift();
  965 + if (req) req.emit('error', err);
  966 + } else {
  967 + // No requests on queue? Where is the request
  968 + assert(0);
  969 + }
  970 + });
956 971
957 972 socket.ondata = function(d, start, end) {
958 973 var ret = parser.execute(d, start, end - start);
@@ -998,6 +1013,8 @@ Agent.prototype._establishNewConnection = function() {
998 1013 var req = socket._httpMessage;
999 1014 assert(req);
1000 1015
  1016 + req.res = res;
  1017 +
1001 1018 // Responses to HEAD requests are AWFUL. Ask Ryan.
1002 1019 // A major oversight in HTTP. Hence this nastiness.
1003 1020 var isHeadResponse = req.method == 'HEAD';
@@ -1100,11 +1117,7 @@ exports.request = function(options, cb) {
1100 1117 var agent = getAgent(options.host, options.port);
1101 1118 var req = agent.appendMessage(options);
1102 1119
1103   - if (cb) {
1104   - req.once('response', function (res) {
1105   - cb(null, res);
1106   - });
1107   - }
  1120 + if (cb) req.once('response', cb);
1108 1121
1109 1122 return req;
1110 1123 };
@@ -1123,6 +1136,7 @@ function Client(port, host) {
1123 1136 this.port = port;
1124 1137 this.host = host;
1125 1138 }
  1139 +util.inherits(Client, EventEmitter);
1126 1140
1127 1141
1128 1142 Client.prototype.request = function(method, path, headers) {
@@ -1141,7 +1155,15 @@ Client.prototype.request = function(method, path, headers) {
1141 1155 host: this.host
1142 1156 };
1143 1157
1144   - return exports.request(options);
  1158 + var self = this;
  1159 + var req = exports.request(options);
  1160 +
  1161 + // proxy error events from req to Client
  1162 + req.on('error', function(err) {
  1163 + self.emit('error', err);
  1164 + });
  1165 +
  1166 + return req;
1145 1167 };
1146 1168
1147 1169

0 comments on commit 4125822

Please sign in to comment.
Something went wrong with that request. Please try again.