From 04d0368baea7c73994d774adc732142fdeec9b41 Mon Sep 17 00:00:00 2001 From: Ben Weaver Date: Sun, 1 May 2011 00:29:02 -0400 Subject: [PATCH] Customizable and in-production proxyError handling. --- lib/node-http-proxy.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/node-http-proxy.js b/lib/node-http-proxy.js index 10b6cfcbb..36481afb9 100644 --- a/lib/node-http-proxy.js +++ b/lib/node-http-proxy.js @@ -358,17 +358,35 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) { // Short-circuits `res` in the event of any error when // contacting the proxy target at `host` / `port`. // - function proxyError(err) { + 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(); } - + outgoing = { host: options.host, port: options.port,