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

Provide production pmx.expressErrorHandler() example #9

Closed
bazineta opened this issue Mar 6, 2015 · 3 comments
Closed

Provide production pmx.expressErrorHandler() example #9

bazineta opened this issue Mar 6, 2015 · 3 comments

Comments

@bazineta
Copy link

bazineta commented Mar 6, 2015

It would be nice to see a production example of the recommended use of pmx.expressErrorHandler().

For example, positioning. Before your own 4-arity functions? After them?

Given that it potentially modifies res.statusCode, I would presume 'after', but I think that a small example here would be very beneficial.

@bazineta
Copy link
Author

bazineta commented Mar 7, 2015

Perhaps using our own setup would be of some use to see if this is worth discussion. Forgive the CoffeeScript if it isn't your thing.

In our case, errors that aren't trivial field validation issues bubble up to the 4-arity handlers for disposition. We've got an error convenience module that wraps the common error codes, and, in the case of anything that'd generate a 500 error, captures a stack trace at the point of instantiation. Therefore any error with a stack present means that Bad Things have happened; any error without one is simply something like a bad request, unauthorized, etc., to send back to the client.

Our setup looks like this. One way to handle the problem, anyway; would be nice to see alternatives, best practices, etc., from the PM2 perspective.

 instance   = express()

  # Various additional middleware setup steps here...

  require('routes') instance

  # No routes matched handler; create a 404 error and send it on to the
  # subsequent handlers for disposition.

  instance.use (req, res, next) -> next new error.NotFound()

  # Return the best status code we can given the provided error and response
  # objects.  If the error has a status set, then use that.  Otherwise, if
  # the response object has a status code, then use that.  If neither object
  # has anything useful, then use 500.
  #
  # If the request is an XHR, then handle it by sending the error status
  # and message to the client; if it's not an XHR, then just render the
  # page for client side processing to handle.
  #
  # If the error contains a stack trace, send it on for further handling,
  # otherwise, we're done here.

  instance.use (err, req, res, next) ->
    res.status err.status or res.statusCode or 500
    if req.xhr
      res.send err.message
    else
      res.render 'index',
        csrf: req.csrfToken()
    return next err if err.stack
    return

  # Error contained a stack trace; report it to the management system.

  instance.use pmx.expressErrorHandler()

  # Final error disposition; error contains a stack trace, so something
  # unexpected has happened.  Log it for posterity just in case the
  # management system wasn't available, and we're done with handling.

  instance.use (err, req, res, next) ->
    console.error err.stack
    return

@rafinskipg
Copy link

+1

@Unitech
Copy link
Member

Unitech commented Feb 27, 2017

@Unitech Unitech closed this as completed Feb 27, 2017
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