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.62k stars 65 forks source link

Promise does not resolve is error occurs in the middle of the handler chain #164

Closed pluby closed 2 years ago

pluby commented 2 years ago

I had an error handling issue and wrote this test for it but was unable to resolve the problem:

  it("reject if there is an callback error with penultimate middleware", () => {
    const handler = nc();
    handler.use((req, res, next) => {
      next(Error("error :("))
    }, (req, res, next) => {
      res.end("good")
    });
    const app = createServer(async (req, res) => {
      try {
        await handler.run(req, res);
        res.end("good");
      } catch (e) {
        // Don't use same text as the error otherwise the default error
        // handler may cause the test to pass.
        res.end("bad");
      }
    });
    return request(app).get("/").expect("bad");
  });

There are two issues revealed by this test:

hoangvvo commented 2 years ago

Sorry for the issue and the late response, this seems to be a misbehavior of the current logic. To fix it temporary, in onError, simply call next(err). I will fix it this week. So sorry again!

hoangvvo commented 2 years ago

This should be fixed in ad857bedb0996312ad3a5ea966ce3a60417429a6. Please reopen if it has not. Thank you.

pluby commented 2 years ago

Thanks.