instantcommerce / next-api-decorators

Collection of decorators to create typed Next.js API routes, with easy request validation and transformation.
https://next-api-decorators.vercel.app
MIT License
415 stars 28 forks source link

@Catch all decorator does not pass through the error thrown, but always is of type `InternalServerErrorException` #551

Closed icazemier closed 1 year ago

icazemier commented 1 year ago

Hi,

I'm using :


    "next": "^13.1.6",
    "next-api-decorators": "^2.0.1",

With a Catch all handler:

const exceptionHandler = (error: unknown, req: NextApiRequest, res: NextApiResponse) => {
  if (!(error instanceof HttpException)) {
    console.warn('Unhandled error for url', error, req?.url);
    res.status(500).send('Unhandled error');
    return;
  }
  res.status(error?.statusCode ?? 500).send(error?.message ?? 'Unhandled');
};

@Catch(exceptionHandler)
class SomeHandler {
   ...
}

But it always passes a InternalServerErrorException: image

While I throw:

        const exception = new HttpException(401, data?.message ?? 'Unknown');
        throw exception;

or for that matter:

        const exception = new UnauthorizedException( data?.message ?? 'Unknown');
        throw exception;

Expected:

The message is passed through, but the status code is not! Sometimes I just want to pass through the http status code together with the message 🙏🏻

ggurkal commented 1 year ago

Hi @icazemier

Can't replicate the issue you're having. Please provide a minimum reproducible repo.

icazemier commented 1 year ago

Oh no! I can't reproduce it either on a dedicated project: https://github.com/icazemier/nextjs-api-decorator-catch-all

Let's close this ticket, excuse me for the fuss.