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

Default error handling #412

Closed
gustavnikolaj opened this issue Jul 9, 2015 · 3 comments
Closed

Default error handling #412

gustavnikolaj opened this issue Jul 9, 2015 · 3 comments

Comments

@gustavnikolaj
Copy link

I encountered a problem with aborting streamed responses - it turned out to be a problem with the way I did the error handling. See expressjs/express#2700 for reference. It would have helped me if the error handling guide had had a mention of the default behavior that is already included in Express. I ended up re-implementing it in my own error handler.

## The Default Error Handler

Express has some default error handling behaviour which it is worth
knowing. If you pass an error to `next` and you do not handle it in
an error handler, it will be written to the client. Including the
stack trace, which is handy for development.

If you call `next` with an error after you have started writing the
response, for instance if you encounter an error while streaming the
response to the client, Express' default error handler will close the
connection and make the request be considered failed.

So when you add a custom error handler you will want to delegate to
the default error handling mechanisms in express, when the headers
have already been sent to the client.

```js
function errorHandler(err, req, res, next) {
  if (res.headersSent) {
    return next(err);
  }
  res.status(500);
  res.render('error', { error: err });
}
    ```

What are your thoughts? The above is just a rough draft, so feel free to take and modify or discard it entirely.

@crandmck
Copy link
Member

Seems like a reasonable addition.... I can tweak the wording, but @hacksparrow can you review the technical content, pls?

@hacksparrow
Copy link
Member

Valid points, added with a little modification - http://expressjs.com/guide/error-handling.html.

@gustavnikolaj
Copy link
Author

That looks really good. Thanks!

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

3 participants