Skip to content

Customizable and production error handling #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

weaver
Copy link

@weaver weaver commented May 1, 2011

Hi,

Right now, if the proxy request produces and error, a JSON stack trace is written out.

This patch adds the ability to completely customize this behavior by listening for a proxyError event. Otherwise, if NODE_ENV=production, the string "Internal Server Error" is written instead of a stack trace. This in-production default behavior mimics Express/Connect. Otherwise, the stack trace is still printed out.

If this implementation isn't suitable, some way to prevent a stack trace in production would be nice.

Best wishes,

-Ben

@indexzero
Copy link
Member

I'm in Portland this week at JSConf / NodeConf, but I will try to take get this merged in. They look good.

@weaver
Copy link
Author

weaver commented May 1, 2011

OK, I just realized this pull request included the other one. I just separated them, but would you prefer them together? Thanks!

indexzero added a commit that referenced this pull request May 19, 2011
@indexzero indexzero closed this May 19, 2011
@indexzero
Copy link
Member

Had to merge this manually because of conflicts with the http.Agent APIs covered in issue #48. Thanks!

@xulfus
Copy link

xulfus commented Jun 15, 2011

Hi, can you provide an example of the proxyError listener? My simple listener does not seem to get fired. If i proxy to an unreachable target, I get the following:

janne bin% curl http://127.0.0.1:1337/test1/index.html
An error has occurred: {"stack":"Error: ECONNREFUSED, Connection refused\n    at Socket._onConnect (net.js:601:18)\n    at IOWatcher.onWritable [as callback] (net.js:186:12)","message":"ECONNREFUSED, Connection refused","errno":61,"code":"ECONNREFUSED","syscall":"connect"}%   

my code snippet:

var pServer = httpproxy.createServer(function (request, response, proxy) {
   // omitted custom logic

    var buffer = proxy.buffer(request);
    proxy.proxyRequest(request, response, {
        port: endpoint.httpPort,
        host: endpoint.address,
        buffer: buffer
    });

});
pServer.listen(1337, "127.0.0.1");
pServer.on('proxyError', function( err, req, res) {
    // does not seem to work
    writelog(err, 'ERROR');
    res.end();
}); 

Best regards, Janne

@indexzero
Copy link
Member

Sure, just added a sample to illustrate this: 4cdbf0e

@xulfus
Copy link

xulfus commented Jun 15, 2011

ahh, of course, server.proxy.on(). Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants