# Prisma common error handle

## install

- `npm i prisma-common-error-handle`

## Why I made this?

- When using Prisma in Nest.js or express or other Frameworks, there are cases where exceptions that Prisma throw are filtered.
- At this time, the library was created to make it easier to deal with the most frequently occurring Prisma errors, such as `PrismaClientKnowRequestError`.
- The `findPrismaErrorInfo()` function provides specific error messages and HTTP status Code that fit each Prisma error.
- In addition, the `PrismaCommonErrCode` enumeration contains a frequently used Prisma error code.
- Each error message contains a detailed description of the error and a solution.
- This library supports All `PrismaClientKnowRequestError`.

## examples

- Below is an example used by the exception filter in Nest.js.
- You can call the function to get the message and HTTP Status Code and handle the exception according to each framework.
- Exception type must be `PrismaClientKnownRequestError`

```typescript
import { ArgumentsHost, Catch } from "@nestjs/common";
import { BaseExceptionFilter } from "@nestjs/core";
import { PrismaClientKnownRequestError } from "@prisma/client";
import { Request, Response } from "express";
import { findPrismaErrorInfo } from "prisma-common-error-handle";

@Catch(PrismaClientKnownRequestError)
export class PrismaClientExceptionFilter extends BaseExceptionFilter {
  catch(exception: PrismaClientKnownRequestError, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<Response>();
    const request = ctx.getRequest<Request>();
    const { message, status } = findPrismaErrorInfo(exception);

    response.status(status).json({
      message,
      statusCode: status,
      timestamp: new Date().toISOString(),
      path: request.url,
    });
  }
}
```

- simple example
- Exception type must be `PrismaClientKnownRequestError`

```typescript
const { message, status } = findPrismaErrorInfo(exception);
```
