Git Product home page Git Product logo

Comments (3)

bazineta avatar bazineta commented on August 23, 2024

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

from pmx.

rafinskipg avatar rafinskipg commented on August 23, 2024

+1

from pmx.

Unitech avatar Unitech commented on August 23, 2024

https://github.com/keymetrics/pmx#add-verbosity-to-an-alert-express-error-handler

from pmx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.