notiz-dev/nestjs-prisma

Nest can't resolve dependencies of the PrismaClientExceptionFilter

sidneip opened this issue · 6 comments

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:

  • Is AppModule a valid NestJS module?
  • If Object is a provider, is it part of the current AppModule?
  • If Object is exported from a separate @module, is that module imported within AppModule?
    @module({
    imports: [ /* the Module containing Object */ ]
    })

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:

  • Is AppModule a valid NestJS module?
  • If Object is a provider, is it part of the current AppModule?
  • If Object is exported from a separate @module, is that module imported within AppModule?
    @module({
    imports: [ /* the Module containing Object */ ]
    })
providers: [
    {
      provide: APP_FILTER,
      useClass: PrismaClientExceptionFilter,
    },
    AppService,
  ],

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

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

import { Prisma } from '@prisma/client';
export type ErrorCodesStatusMapping = {
[key: string]: number;
};
/**
* {@link PrismaClientExceptionFilter} catches {@link Prisma.PrismaClientKnownRequestError} and {@link Prisma.NotFoundError} exceptions.
*/
@Catch(Prisma?.PrismaClientKnownRequestError, Prisma?.NotFoundError)

No, i did setup default.

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

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.

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

Thanks @marcjulian

I did one test and i have success.