-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandle-prisma-errors.ts
More file actions
31 lines (30 loc) · 1.36 KB
/
handle-prisma-errors.ts
File metadata and controls
31 lines (30 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import type { PrismaErrorLike } from '@api/types/prisma-error-like.type';
import type { HttpError } from '@shared/errors/http-error';
import { InternalServerError } from '@api/errors/http/internal-server.error';
import { ResourceAlreadyExistsError } from '@api/errors/http/prisma/resource-already-exists.error';
import { ResourceNotFoundError } from '@api/errors/http/prisma/resource-not-found.error';
import { RequestedValueTooLongError } from '@api/errors/http/prisma/requested-value-too-long.error';
import { InvalidRequestDataError } from '@api/errors/http/prisma/invalid-request-data.error';
import { ApiLogger } from '@api/logs/api.logger';
export function handlePrismaError(error: PrismaErrorLike): HttpError {
if (error.name === 'PrismaClientKnownRequestError') {
switch (error.code) {
case 'P2002':
return new ResourceAlreadyExistsError();
case 'P2025':
return new ResourceNotFoundError();
case 'P2000':
return new RequestedValueTooLongError();
default:
ApiLogger.error(
'PrismaClientKnownRequestError not handled with code ' + error.code
);
return new InternalServerError();
}
}
if (error.name === 'PrismaClientValidationError') {
return new InvalidRequestDataError();
}
ApiLogger.error('Error while handling prisma errors: ', error);
return new InternalServerError();
}