From 1d81325696076038483a3c30a93962d0d91afb23 Mon Sep 17 00:00:00 2001 From: Yiming Date: Fri, 23 Aug 2024 21:30:28 -0700 Subject: [PATCH] fix(redwood): fix incorrect error type thrown (#1659) --- packages/misc/redwood/src/graphql.ts | 5 ++--- packages/runtime/src/constants.ts | 2 +- packages/runtime/src/enhancements/policy/policy-utils.ts | 2 +- packages/server/src/api/rest/index.ts | 2 +- packages/server/src/api/rpc/index.ts | 2 +- 5 files changed, 6 insertions(+), 7 deletions(-) diff --git a/packages/misc/redwood/src/graphql.ts b/packages/misc/redwood/src/graphql.ts index 69267b5e2..943329744 100644 --- a/packages/misc/redwood/src/graphql.ts +++ b/packages/misc/redwood/src/graphql.ts @@ -1,9 +1,8 @@ -import { ForbiddenError } from '@redwoodjs/graphql-server'; +import { ForbiddenError, ValidationError } from '@redwoodjs/graphql-server'; import { CrudFailureReason, EnhancementOptions, PrismaErrorCode, - ValidationError, enhance, isPrismaClientKnownRequestError, type AuthUser, @@ -47,7 +46,7 @@ export function useZenStack( // Transforms ZenStack errors into appropriate RedwoodJS errors function transformError(error: unknown) { - if (isPrismaClientKnownRequestError(error) && error.code === PrismaErrorCode.CONSTRAINED_FAILED) { + if (isPrismaClientKnownRequestError(error) && error.code === PrismaErrorCode.CONSTRAINT_FAILED) { if ( error.meta?.reason === CrudFailureReason.ACCESS_POLICY_VIOLATION || error.meta?.reason === CrudFailureReason.RESULT_NOT_READABLE diff --git a/packages/runtime/src/constants.ts b/packages/runtime/src/constants.ts index 5fd8c2901..36acf8c83 100644 --- a/packages/runtime/src/constants.ts +++ b/packages/runtime/src/constants.ts @@ -40,7 +40,7 @@ export enum PrismaErrorCode { /** * A constraint failed on the database */ - CONSTRAINED_FAILED = 'P2004', + CONSTRAINT_FAILED = 'P2004', /** * The required connected records were not found diff --git a/packages/runtime/src/enhancements/policy/policy-utils.ts b/packages/runtime/src/enhancements/policy/policy-utils.ts index f01bebd52..2df2ea2e2 100644 --- a/packages/runtime/src/enhancements/policy/policy-utils.ts +++ b/packages/runtime/src/enhancements/policy/policy-utils.ts @@ -1215,7 +1215,7 @@ export class PolicyUtil extends QueryUtils { reason?: CrudFailureReason, zodErrors?: ZodError ) { - const args: any = { clientVersion: getVersion(), code: PrismaErrorCode.CONSTRAINED_FAILED, meta: {} }; + const args: any = { clientVersion: getVersion(), code: PrismaErrorCode.CONSTRAINT_FAILED, meta: {} }; if (reason) { args.meta.reason = reason; } diff --git a/packages/server/src/api/rest/index.ts b/packages/server/src/api/rest/index.ts index 022ac72a7..75b2f738b 100644 --- a/packages/server/src/api/rest/index.ts +++ b/packages/server/src/api/rest/index.ts @@ -1589,7 +1589,7 @@ class RequestHandler extends APIHandlerBase { private handlePrismaError(err: unknown) { if (isPrismaClientKnownRequestError(err)) { - if (err.code === PrismaErrorCode.CONSTRAINED_FAILED) { + if (err.code === PrismaErrorCode.CONSTRAINT_FAILED) { if (err.meta?.reason === CrudFailureReason.DATA_VALIDATION_VIOLATION) { return this.makeError( 'validationError', diff --git a/packages/server/src/api/rpc/index.ts b/packages/server/src/api/rpc/index.ts index a8882b8be..a1a71c1c6 100644 --- a/packages/server/src/api/rpc/index.ts +++ b/packages/server/src/api/rpc/index.ts @@ -18,7 +18,7 @@ import { logError, registerCustomSerializers } from '../utils'; registerCustomSerializers(); const ERROR_STATUS_MAPPING: Record = { - [PrismaErrorCode.CONSTRAINED_FAILED]: 403, + [PrismaErrorCode.CONSTRAINT_FAILED]: 403, [PrismaErrorCode.REQUIRED_CONNECTED_RECORD_NOT_FOUND]: 404, [PrismaErrorCode.DEPEND_ON_RECORD_NOT_FOUND]: 404, };