notiz-dev / nestjs-prisma

Easy Prisma support for your NestJS application
https://nestjs-prisma.dev
MIT License
572 stars 49 forks source link

Nest can't resolve dependencies of the PrismaClientExceptionFilter #51

Closed sidneip closed 1 year ago

sidneip commented 1 year ago

i am getting error when i am configure PrismaClientExceptionFilter in app.module.ts

[Nest] 32200 - 01/02/2023, 11:14:18 LOG [NestFactory] Starting Nest application... [Nest] 32200 - 01/02/2023, 11:14:18 LOG [InstanceLoader] PrismaModule dependencies initialized +76ms [Nest] 32200 - 01/02/2023, 11:14:18 ERROR [ExceptionHandler] Nest can't resolve dependencies of the PrismaClientExceptionFilter (?, Object). Please make sure that the argument Object at index [0] is available in the AppModule context.

Potential solutions:

Error: Nest can't resolve dependencies of the PrismaClientExceptionFilter (?, Object). Please make sure that the argument Object at index [0] is available in the AppModule context.

Potential solutions:

providers: [
    {
      provide: APP_FILTER,
      useClass: PrismaClientExceptionFilter,
    },
    AppService,
  ],
marcjulian commented 1 year ago

Hi @sidneip are you generating the Prisma Client to a custom location?

PrismaClientExceptionFilter relies on @prisma/client to specify the exceptions to handle.

https://github.com/notiz-dev/nestjs-prisma/blob/a3aebd9b2a1d1df9c55e5fddf0257893f68ffd05/lib/prisma-client-exception.filter.ts#L9-L18

sidneip commented 1 year ago

No, i did setup default.

marcjulian commented 1 year ago

Did you run npx prisma generate? Maybe the prisma client hasn't been generated.

marcjulian commented 1 year ago

Nevermind, I tried it out again and also got the same error as you experiencing.

If you want to use APP_FILTER you need to us a factory instead of the class

providers: [
    AppService,
    {
      provide: APP_FILTER,
      useFactory: ({ httpAdapter }: HttpAdapterHost) => {
        return new PrismaClientExceptionFilter(httpAdapter);
      },
      inject: [HttpAdapterHost],
    },
  ],

Alternative would be to instantiate PrismaClientExceptionFilter in main.ts. I will update the docs. Thanks for reporting the bug.

marcjulian commented 1 year ago

I updated the docs for applying APP_FILTER correctly 👉 https://nestjs-prisma.dev/docs/exception-filter/#app_filter

sidneip commented 1 year ago

Thanks @marcjulian

I did one test and i have success.