Implement prisma error handling
This commit is contained in:
parent
ee6dd16be2
commit
fa26595797
@ -12,75 +12,29 @@ const prisma = new PrismaClient({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// FIXME: any
|
// 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);
|
// P2002 -> "Unique constraint failed on the {constraint}"
|
||||||
res.status(500).json({ status: 'ERROR', meta: errorObj.meta, errorcode: errorObj.code, message: errorObj.message });
|
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)
|
default:
|
||||||
|
res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', message: 'An error occurred during the database operation' });
|
||||||
// switch (errorObj.code) {
|
break;
|
||||||
// // P2002 -> "Unique constraint failed on the {constraint}"
|
}
|
||||||
// // https://www.prisma.io/docs/reference/api-reference/error-reference
|
} else {
|
||||||
// case 'P2002': //
|
res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', message: 'If you can read this something went terribly wrong!' });
|
||||||
// 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' });
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default prisma;
|
export default prisma;
|
||||||
|
@ -21,7 +21,6 @@ import routes from './routes/index.js';
|
|||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
|
|
||||||
log.core.trace("Running from path: " + __path);
|
log.core.trace("Running from path: " + __path);
|
||||||
db.$disconnect();
|
|
||||||
|
|
||||||
|
|
||||||
// MARK: Express
|
// MARK: Express
|
||||||
|
@ -34,6 +34,8 @@ async function get(req: Request, res: Response) {
|
|||||||
} else {
|
} else {
|
||||||
res.status(404).json({ status: 'ERROR', errorcode: 'NOT_FOUND', message: 'Could not find specified object' });
|
res.status(404).json({ status: 'ERROR', errorcode: 'NOT_FOUND', message: 'Could not find specified object' });
|
||||||
}
|
}
|
||||||
|
}).catch((err) => {
|
||||||
|
handlePrismaError(err, res, 'GET alertContact');
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// count all entrys
|
// count all entrys
|
||||||
@ -47,6 +49,8 @@ async function get(req: Request, res: Response) {
|
|||||||
})
|
})
|
||||||
.then((result) => {
|
.then((result) => {
|
||||||
res.status(200).json(result);
|
res.status(200).json(result);
|
||||||
|
}).catch((err) => {
|
||||||
|
handlePrismaError(err, res, 'GET alertContact');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -62,12 +66,16 @@ async function get(req: Request, res: Response) {
|
|||||||
} else {
|
} else {
|
||||||
res.status(404).json({ status: 'ERROR', errorcode: 'NOT_FOUND', message: 'Could not find specified object' });
|
res.status(404).json({ status: 'ERROR', errorcode: 'NOT_FOUND', message: 'Could not find specified object' });
|
||||||
}
|
}
|
||||||
|
}).catch((err) => {
|
||||||
|
handlePrismaError(err, res, 'GET alertContact');
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// count all entrys without FullTextSearch
|
// count all entrys without FullTextSearch
|
||||||
log.api?.trace('count all entrys - without FullTextSearch');
|
log.api?.trace('count all entrys - without FullTextSearch');
|
||||||
await db.alertContacts.count().then((result) => {
|
await db.alertContacts.count().then((result) => {
|
||||||
res.status(200).json(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) => {
|
.then((result) => {
|
||||||
res.status(201).json({ status: 'CREATED', message: 'Successfully created alertContact', id: result.id });
|
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) => {
|
.then((result) => {
|
||||||
res.status(200).json({ status: 'UPDATED', message: 'Successfully updated alertContact', id: result.id });
|
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) => {
|
.then((result) => {
|
||||||
res.status(200).json({ status: 'DELETED', message: 'Successfully deleted alertContact', id: result.id });
|
res.status(200).json({ status: 'DELETED', message: 'Successfully deleted alertContact', id: result.id });
|
||||||
|
}).catch((err) => {
|
||||||
|
handlePrismaError(err, res, 'DEL alertContact');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user