Add scouthub-magazzino-be
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { HttpError } from '../errors';
|
||||
|
||||
// Converte gli errori noti di Prisma (vincoli FK, record non trovato) in HttpError
|
||||
// con uno status code sensato, senza dover ripetere lo stesso catch ovunque.
|
||||
export function toHttpError(err: unknown, notFoundMessage: string, conflictMessage: string): unknown {
|
||||
if (err instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
if (err.code === 'P2025') {
|
||||
return new HttpError(404, notFoundMessage);
|
||||
}
|
||||
if (err.code === 'P2003') {
|
||||
return new HttpError(409, conflictMessage);
|
||||
}
|
||||
}
|
||||
return err;
|
||||
}
|
||||
Reference in New Issue
Block a user