hoangvvo / next-connect

The TypeScript-ready, minimal router and middleware layer for Next.js, Micro, Vercel, or Node.js http/http2
https://www.npmjs.com/package/next-connect
MIT License
1.64k stars 65 forks source link

Add support for catching async handler errors #41

Closed dheeraj-p closed 4 years ago

dheeraj-p commented 4 years ago

Currently next-connect has onError option to catch errors thrown by handlers and send relevant response. But it doesn't work with async handlers. What I would like to have is error thrown from a async handler should get caught in onError without have to write try/catch blocks and then pass the error to next(Error) function.

import nextConnect from 'next-connect';
handler = nextConnect({ onError });

handler.use(async (req, res) => {
  throw new Error("Something failed");
});
hoangvvo commented 4 years ago

@dheeraj-p Thanks for reporting! Fixed in 0.6.2.

dheeraj-p commented 4 years ago

Thank you so much