Skip to content

Add model info to PrismaClientKnownRequestError #20724

@EizFeren

Description

@EizFeren

Problem

I have global exception filter for NestJS, and it's catcher only have error instance of PrismaClientKnownRequestError. This execption filter needs somehow to know, what model is related to this error, e.g. to provide custom error message, related to model name.

Real example - trying to create user, but user with provided email exists in database. Exception filter uses error code (P2002) to detect this is unique constraint error or something else, but it still doesn't know what model it's related for.

Suggested solution

Add model name to error metadata, e.g.:

{
  meta: {
    model: 'User',
    target: [
      'email'
    ]
  }
}

Alternatives

Imho best approach will be attaching custom metadata for any query, e.g.:

prismaClient.user.create({
  data: {
    ...
  },
  customMetadata: {
    ... // any data engineer wants to attach
  },
});

Idk how it's possible and/or type safe, but this grants far more flexibility than current usage.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions