From 28f739f362b9f95652d6ee49d6b43285be6ce7ff Mon Sep 17 00:00:00 2001 From: imbharat420 Date: Sun, 4 Jun 2023 23:12:18 +0530 Subject: [PATCH] prisma error codes update for multiple errors --- src/error/errorMessages.ts | 4 +++ src/error/getErrorInfoHandlers.ts | 11 +++---- src/error/prismaErrorMessages.ts | 49 +++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 src/error/prismaErrorMessages.ts diff --git a/src/error/errorMessages.ts b/src/error/errorMessages.ts index 258333e..f4642c3 100644 --- a/src/error/errorMessages.ts +++ b/src/error/errorMessages.ts @@ -1,3 +1,5 @@ +import PrismaErrorMessages from './prismaErrorMessages' + export default { statusCode: 400, notFound: ["Oops, looks like you're lost in space!", 404], @@ -16,6 +18,8 @@ export default { prismaNotFound: ['Invalid input for `{$}`', 400], + ...PrismaErrorMessages(), + checkType: [`Expected '{$key}' should be '{$type}'`, 400], checkRequired: [`Value for '{$key}' is required`, 400], } as const diff --git a/src/error/getErrorInfoHandlers.ts b/src/error/getErrorInfoHandlers.ts index c0af09d..6b52888 100644 --- a/src/error/getErrorInfoHandlers.ts +++ b/src/error/getErrorInfoHandlers.ts @@ -6,7 +6,7 @@ import { import ReqError from '../ReqError' import { as, replacer } from '../utils' import { GetErrorInfoHandler } from '../types' - +import {PrismaError} from './prismaErrorMessages' /** Add a function to the array, and make your logic Do not forget to return [string, number] or void @@ -83,12 +83,9 @@ export default as([ //Prisma Error function (err) { - switch (err.code) { - case 'P1012': - return replacer(this.prismaDuplicate, '{$key}', err.meta.target.join(', ')); - case 'P2025': - return replacer(this.prismaNotFound, '{$key}', err.meta.target.join(', ')); - } + if(PrismaError[err.code]){ + return [PrismaError[err.code], this.[err.code][1]] + } }, // ReqError CheckType diff --git a/src/error/prismaErrorMessages.ts b/src/error/prismaErrorMessages.ts new file mode 100644 index 0000000..5f0e0e8 --- /dev/null +++ b/src/error/prismaErrorMessages.ts @@ -0,0 +1,49 @@ +export const PrismaError:{ + [key: string]: string +} = { + P2000: "The provided value for the column is too long for the column's type. Column: {column_name}", + P2001: 'The record searched for in the where condition ({model_name}.{argument_name} = {argument_value}) does not exist', + P2002: 'Unique constraint failed on the {constraint}', + P2003: 'Foreign key constraint failed on the field: {field_name}', + P2004: 'A constraint failed on the database: {database_error}', + P2005: 'The value stored in the database for the field is invalid for the field\'s type', + P2006: 'The provided value for field is not valid', + P2007: 'Data validation error', + P2008: 'Failed to parse the query at', + P2009: 'Failed to validate the query', + P2010: 'Raw query failed', + P2011: 'Null constraint violation on the {constraint}', + P2012: 'Missing a required value at', + P2013: 'Missing the required argument for field', + P2014: 'The change you are trying to make would violate the required relation', + P2015: 'A related record could not be found', + P2016: 'Query interpretation error', + P2017: 'The records for relation between the and models are not connected', + P2018: 'The required connected records were not found', + P2019: 'Input error {details}', + P2020: 'Value out of range for the type', + P2021: 'The table does not exist in the current database', + P2022: 'The column does not exist in the current database', + P2023: 'Inconsistent column data', + P2024: 'Timed out fetching a new connection from the connection pool', + P2025: 'An operation failed because it depends on one or more records that were required but not found', + P2026: 'The current database provider doesn\'t support a feature that the query used', + P2027: 'Multiple errors occurred on the database during query execution', + P2028: 'Transaction API error', + P2030: 'Cannot find a fulltext index to use for the search, try adding a @@fulltext([Fields...]) to your schema', + P2031: "Prisma needs to perform transactions, which requires your MongoDB server to be run as a replica set. See details: https://pris.ly/d/mongodb-replica-set", + P2033: "A number used in the query does not fit into a 64 bit signed integer. Consider using BigInt as field type if you're trying to store large integers", + P2034: "Transaction failed due to a write conflict or a deadlock. Please retry your transaction" +} + + +const PrismaErrorMessages = () => { + let errors: any = {} + Object.keys(PrismaError).forEach((key) => { + errors[key] = [PrismaError[key], 400] + }) + return errors +} + + +export default PrismaErrorMessages() \ No newline at end of file