poppinss / youch

Pretty error reporting for Node.js :rocket:

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use Youch with express?

brunoti opened this issue · comments

Don't know. I don't use express.

Thanks man!

commented
const Youch = require('youch');

// after routes
app.use((error, req, res, next) => {
  if (error && process.env.NODE_ENV !== 'production' && !res.headersSent) {
    const youch = new Youch(error, req);

    return youch
      .toHTML()
      .then(html => (
        res
          .status(500)
          .set('Content-Type', 'text/html')
          .send(html)
      ))
  }
  next(error);
});

@ajitid You probably want to set the status code to 500 or something as well.

commented

Thanks @hmil!

Anything else you think which I can add to improve this code?