Skip to content

Commit 652cca3

Browse files
committed
[api] Manual merge of #46: add custom proxyError event and enable production error handling.
1 parent bd45216 commit 652cca3

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

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

+19-1
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,28 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) {
370370
//
371371
function proxyError(err) {
372372
errState = true;
373+
374+
//
375+
// Emit an `error` event, allowing the application to use custom
376+
// error handling. The error handler should end the response.
377+
//
378+
if (self.emit('proxyError', err, req, res)) {
379+
return;
380+
}
381+
373382
res.writeHead(500, { 'Content-Type': 'text/plain' });
374383

375384
if (req.method !== 'HEAD') {
376-
res.write('An error has occurred: ' + JSON.stringify(err));
385+
//
386+
// This NODE_ENV=production behavior is mimics Express and
387+
// Connect.
388+
//
389+
if (process.env.NODE_ENV === 'production') {
390+
res.write('Internal Server Error');
391+
}
392+
else {
393+
res.write('An error has occurred: ' + JSON.stringify(err));
394+
}
377395
}
378396

379397
res.end();

0 commit comments

Comments
 (0)