From fa26595797f3893b632d52deb6015eeb891a044a Mon Sep 17 00:00:00 2001 From: Spacelord Date: Sun, 2 Feb 2025 18:13:02 +0100 Subject: [PATCH] Implement prisma error handling --- src/handlers/db.ts | 84 +++++++----------------------- src/index.ts | 1 - src/routes/api/v1/alertContacts.ts | 14 +++++ 3 files changed, 33 insertions(+), 66 deletions(-) diff --git a/src/handlers/db.ts b/src/handlers/db.ts index 3341ffe..c521f43 100644 --- a/src/handlers/db.ts +++ b/src/handlers/db.ts @@ -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; diff --git a/src/index.ts b/src/index.ts index 239a483..43f688a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,7 +21,6 @@ import routes from './routes/index.js'; import fs from 'node:fs'; log.core.trace("Running from path: " + __path); -db.$disconnect(); // MARK: Express diff --git a/src/routes/api/v1/alertContacts.ts b/src/routes/api/v1/alertContacts.ts index 4aff399..c88591b 100644 --- a/src/routes/api/v1/alertContacts.ts +++ b/src/routes/api/v1/alertContacts.ts @@ -34,6 +34,8 @@ async function get(req: Request, res: Response) { } else { res.status(404).json({ status: 'ERROR', errorcode: 'NOT_FOUND', message: 'Could not find specified object' }); } + }).catch((err) => { + handlePrismaError(err, res, 'GET alertContact'); }); } else { // count all entrys @@ -47,6 +49,8 @@ async function get(req: Request, res: Response) { }) .then((result) => { res.status(200).json(result); + }).catch((err) => { + handlePrismaError(err, res, 'GET alertContact'); }); } } else { @@ -62,12 +66,16 @@ async function get(req: Request, res: Response) { } else { res.status(404).json({ status: 'ERROR', errorcode: 'NOT_FOUND', message: 'Could not find specified object' }); } + }).catch((err) => { + handlePrismaError(err, res, 'GET alertContact'); }); } else { // count all entrys without FullTextSearch log.api?.trace('count all entrys - without FullTextSearch'); await db.alertContacts.count().then((result) => { res.status(200).json(result); + }).catch((err) => { + handlePrismaError(err, res, 'GET alertContact'); }); } } @@ -95,6 +103,8 @@ async function post(req: Request, res: Response) { }) .then((result) => { res.status(201).json({ status: 'CREATED', message: 'Successfully created alertContact', id: result.id }); + }).catch((err) => { + handlePrismaError(err, res, 'POST alertContact'); }); } } @@ -123,6 +133,8 @@ async function patch(req: Request, res: Response) { }) .then((result) => { res.status(200).json({ status: 'UPDATED', message: 'Successfully updated alertContact', id: result.id }); + }).catch((err) => { + handlePrismaError(err, res, 'PATCH alertContact'); }); } } @@ -143,6 +155,8 @@ async function del(req: Request, res: Response) { }) .then((result) => { res.status(200).json({ status: 'DELETED', message: 'Successfully deleted alertContact', id: result.id }); + }).catch((err) => { + handlePrismaError(err, res, 'DEL alertContact'); }); } }