Skip to content
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

Request for assistance with errorHandler() #503

Closed
boozedog opened this issue Mar 6, 2012 · 2 comments
Closed

Request for assistance with errorHandler() #503

boozedog opened this issue Mar 6, 2012 · 2 comments

Comments

@boozedog
Copy link

boozedog commented Mar 6, 2012

Hi there, I'm playing with errorHandler and having some trouble figuring it out.

Consider the following code:
var connect = require('connect');
var app = connect()
//.use(connect.errorHandler())
.use(function(req, res) {
var x = JSON.stringify(req); // this line will crash
res.write('this is a test');
res.end(req.url);
})
.listen(8080);


When I run this, and visit the page, I get a text/plain stacktrace along with a 500 HTTP error code. So far so good.

BUT when I un-comment the `.use(connect.errorHandler())` I don't see any change in the behavior of the app ... still seeing text/plain stacktrace. Was expecting pretty HTML stacktrace or something?

What's more, if I swap two lines:
        .use(function(req, res) {
            res.write('this is a test');
            var x = JSON.stringify(req); // this line will crash
            res.end(req.url);
        })

Now the app outputs nothing to the browser at all (but I still see the stack trace in the console.log).

I even threw a couple console.log statements into node_modules/connect/lib/middleware/errorHandler.js to see what's going on but it doesn't appear the code in that middleware ever executes.

I've tried The Google but not finding much in the way of documentation ... can anyone assist?

Thanks!

PS. Apologies for the code formatting ... GFM isn't working for me this morning?
@tj
Copy link
Member

tj commented Mar 6, 2012

you just need to place it below any middleware that you want to "cover" with the error handling, since it passes downstream not upstream. ex:

app.use(foo);
app.use(bar);
app.use(connect.errorHandler());
app.use(baz); // if this has an error Connect will deal with it, but not errorHandler()

@tj tj closed this as completed Mar 6, 2012
@boozedog
Copy link
Author

boozedog commented Mar 6, 2012

That did the trick! Thanks very much.

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

No branches or pull requests

2 participants