diff --git a/lib/node-http-proxy.js b/lib/node-http-proxy.js index 938e9bf..6711b19 100644 --- a/lib/node-http-proxy.js +++ b/lib/node-http-proxy.js @@ -370,10 +370,28 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) { // function proxyError(err) { errState = true; + + // + // Emit an `error` event, allowing the application to use custom + // error handling. The error handler should end the response. + // + if (self.emit('proxyError', err, req, res)) { + return; + } + res.writeHead(500, { 'Content-Type': 'text/plain' }); if (req.method !== 'HEAD') { - res.write('An error has occurred: ' + JSON.stringify(err)); + // + // This NODE_ENV=production behavior is mimics Express and + // Connect. + // + if (process.env.NODE_ENV === 'production') { + res.write('Internal Server Error'); + } + else { + res.write('An error has occurred: ' + JSON.stringify(err)); + } } res.end();