Skip to content

Commit e1c41d0

Browse files
committed
[fix] Add guards to every throw-able res.end call
1 parent de4a6fe commit e1c41d0

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

Diff for: lib/node-http-proxy/http-proxy.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
178178
}
179179
}
180180

181-
res.end();
181+
try { res.end() }
182+
catch (ex) { console.error("res.end error: %s", ex.message) }
182183
}
183184

184185
//
@@ -209,7 +210,9 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
209210

210211
// If `response.statusCode === 304`: No 'data' event and no 'end'
211212
if (response.statusCode === 304) {
212-
return res.end();
213+
try { res.end() }
214+
catch (ex) { console.error("res.end error: %s", ex.message) }
215+
return;
213216
}
214217

215218
//
@@ -223,9 +226,10 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
223226
try {
224227
res.write(chunk);
225228
}
226-
catch (er) {
229+
catch (ex) {
230+
console.error("res.write error: %s", ex.message);
227231
try { res.end() }
228-
catch (er) {}
232+
catch (ex) { console.error("res.write error: %s", ex.message) }
229233
}
230234
}
231235
});
@@ -240,8 +244,10 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
240244
response.on('end', function () {
241245
if (!errState) {
242246
reverseProxy.removeListener('error', proxyError);
243-
res.end();
244-
247+
248+
try { res.end() }
249+
catch (ex) { console.error("res.end error: %s", ex.message) }
250+
245251
// Emit the `end` event now that we have completed proxying
246252
self.emit('end', req, res);
247253
}

0 commit comments

Comments
 (0)