Implement prisma error handling
This commit is contained in:
@ -12,75 +12,29 @@ const prisma = new PrismaClient({
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// FIXME: any
|
||||
export function handlePrismaError(errorObj: any, res: Response) {
|
||||
export function handlePrismaError(errorObj: any, res: Response, source: string) {
|
||||
log.db.error(source, errorObj);
|
||||
if (errorObj instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
switch (errorObj.code) {
|
||||
|
||||
log.core.debug(errorObj);
|
||||
res.status(500).json({ status: 'ERROR', meta: errorObj.meta, errorcode: errorObj.code, message: errorObj.message });
|
||||
// P2002 -> "Unique constraint failed on the {constraint}"
|
||||
case 'P2002':
|
||||
res.status(409).json({ status: 'ERROR', errorcode: 'DB_ERROR', message: 'The object needs to be unique', meta: errorObj.meta });
|
||||
break;
|
||||
|
||||
// P2003 -> "Foreign key constraint failed on the field: {field_name}"
|
||||
case 'P2003':
|
||||
res.status(404).json({ status: 'ERROR', errorcode: 'DB_ERROR', message: 'Relation object does not exist', meta: errorObj.meta });
|
||||
break;
|
||||
|
||||
// if(errorObj instanceof Prisma.PrismaClientKnownRequestError)
|
||||
|
||||
// switch (errorObj.code) {
|
||||
// // P2002 -> "Unique constraint failed on the {constraint}"
|
||||
// // https://www.prisma.io/docs/reference/api-reference/error-reference
|
||||
// case 'P2002': //
|
||||
// log.db.error('');
|
||||
// break;
|
||||
|
||||
|
||||
// // P2003 -> "Foreign key constraint failed on the field: {field_name}"
|
||||
// // https://www.prisma.io/docs/reference/api-reference/error-reference
|
||||
// // FIXME: Is this errormessage right?
|
||||
// case 'P2003': //
|
||||
// log.db.error('');
|
||||
// break;
|
||||
// case 'xxx': //
|
||||
// log.db.error('');
|
||||
// break;
|
||||
// case 'xxx':
|
||||
// log.db.error('');
|
||||
// break;
|
||||
// case 'xxx':
|
||||
// log.db.error('');
|
||||
// break;
|
||||
// case 'xxx':
|
||||
// log.db.error('');
|
||||
// break;
|
||||
// case 'xxx':
|
||||
// log.db.error('');
|
||||
// break;
|
||||
// case 'xxx':
|
||||
// log.db.error('');
|
||||
// break;
|
||||
// case 'xxx':
|
||||
// log.db.error('');
|
||||
// break;
|
||||
|
||||
|
||||
// default:
|
||||
// break;
|
||||
// }
|
||||
|
||||
// // Check if an entry already exists.
|
||||
// if (errorcode === 'P2002') {
|
||||
// // P2002 -> "Unique constraint failed on the {constraint}"
|
||||
// // https://www.prisma.io/docs/reference/api-reference/error-reference
|
||||
// res.status(409).json({ status: 'ERROR', errorcode: 'EXISTING', message: 'Item already exists' });
|
||||
// } else if (errorcode == 'P2003') {
|
||||
// // P2003 -> "Foreign key constraint failed on the field: {field_name}"
|
||||
// // https://www.prisma.io/docs/reference/api-reference/error-reference
|
||||
// // FIXME: Is this errormessage right?
|
||||
// res.status(404).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'Item does not exist' });
|
||||
// } else if (errorcode == 'P2000') {
|
||||
// // P2000 -> "The provided value for the column is too long for the column's type. Column: {column_name}"
|
||||
// // https://www.prisma.io/docs/reference/api-reference/error-reference
|
||||
// res.status(404).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: 'One or more fields exceed the maximum length restriction' });
|
||||
// } else {
|
||||
// log.db.error(err);
|
||||
// res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' });
|
||||
// }
|
||||
default:
|
||||
res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', message: 'An error occurred during the database operation' });
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', message: 'If you can read this something went terribly wrong!' });
|
||||
}
|
||||
}
|
||||
|
||||
export default prisma;
|
||||
|
Reference in New Issue
Block a user