kiyasov / platform-hono

About Hono Http adapter for Nest.js
MIT License
73 stars 5 forks source link

Exception handler does not work #4

Closed 95gabor closed 2 weeks ago

95gabor commented 1 month ago

Hi!

I tried to use the exception handler from the README, but not worked for me.

I had to create a new Response object, because the previous error instance had JSON parse error (I only throw a simple new Error() instance in my controller). Here is my solution:

import { HonoRequest } from '@kiyasov/platform-hono';
import {
  ExceptionFilter,
  Catch,
  ArgumentsHost,
  HttpException,
  InternalServerErrorException,
} from '@nestjs/common';
import { Context } from 'hono';

@Catch()
export class AllExceptionFilter implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<Context>();
    const request = ctx.getRequest<HonoRequest>();

    const status =
      exception.getStatus?.() || new InternalServerErrorException().getStatus();

    response.res = Response.json(
      {
        statusCode: status,
        timestamp: new Date().toISOString(),
        path: request.url,
      },
      { status },
    );
  }
}
kiyasov commented 1 month ago

Hi! Did you enable it via useGlobalFilters? https://github.com/kiyasov/platform-hono/blob/dba04ff2e8f11e082f758d6a42e3035a9f6818d2/example/src/main.ts#L26

95gabor commented 1 month ago

Hi! Did you enable it via useGlobalFilters?

https://github.com/kiyasov/platform-hono/blob/dba04ff2e8f11e082f758d6a42e3035a9f6818d2/example/src/main.ts#L26

Yes, but unfortunately it did not work.

kiyasov commented 3 weeks ago

Can you share the exmaple code at https://replit.com ?

95gabor commented 2 weeks ago

Can you share the exmaple code at https://replit.com ?

Sorry, it seems to work here, so something wrong in my environment.

Here is the working example: https://replit.com/@95gabor/hono-nest-esbuild-example