2
AlgoRythm
295d

Something annoying about the Node.JS and express stack: no way to globally catch errors using middleware.

I mean, you can try. There's even specific middleware designed for it. However, it only catches synchronous errors. So basically only controllers that don't fetch or upload any sort of data and just return some static view or whatever.

ASP.Net middleware chain is so much more robust :(

Comments
  • 0
    lol git gud
  • 2
    process.on('uncaughtException', err => {
    console.error(err && err.stack)
    });

    this works. or do you mean sth else?

    well its not middleware :/
  • 1
    Express does have a four argument version of handlers, (err, req, res, next) that serves exactly for that.

    You also have the express-async-errors package to deal with async errors.
  • 0
    @joewilliams007 Yeah I'm complaining about the middleware chain being insufficient

    @CoreFusionX I know, I'm using error handling middleware. Why do I need yet another dependency to catch asyc though? Most of my fuckin controllers are async because of database access.
  • 0
    Because the package monkey patches express to basically all a try catch to handler invocations.

    Express 5 does it in its own, but since they are slow as ass to actually deploy it, you gotta do what you gotta do.
Add Comment