From 99cba74fd2836031db8e04b6bd7cab67e4e803e6 Mon Sep 17 00:00:00 2001 From: Edoardo Ranghieri Date: Sat, 20 Jul 2024 02:15:29 +0200 Subject: [PATCH] refactor: check server validation errors with `ActionServerValidationError` instance --- packages/next-safe-action/src/action-builder.ts | 11 ++--------- packages/next-safe-action/src/validation-errors.ts | 2 -- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/packages/next-safe-action/src/action-builder.ts b/packages/next-safe-action/src/action-builder.ts index 2229979a..85ef67f0 100644 --- a/packages/next-safe-action/src/action-builder.ts +++ b/packages/next-safe-action/src/action-builder.ts @@ -14,7 +14,7 @@ import type { StateServerCodeFn, } from "./index.types"; import { ActionMetadataError, DEFAULT_SERVER_ERROR_MESSAGE, isError } from "./utils"; -import { ActionValidationError, buildValidationErrors } from "./validation-errors"; +import { ActionServerValidationError, ActionValidationError, buildValidationErrors } from "./validation-errors"; import type { BindArgsValidationErrors, HandleBindArgsValidationErrorsShapeFn, @@ -223,14 +223,7 @@ export function actionBuilder< } // If error is `ActionServerValidationError`, return `validationErrors` as if schema validation would fail. - // TODO: check if this is still true after switching to built-in system - if ( - e instanceof Error && - "kind" in e && - "validationErrors" in e && - typeof e.kind === "string" && - e.kind === "__actionServerValidationError" - ) { + if (e instanceof ActionServerValidationError) { const ve = e.validationErrors as ValidationErrors; middlewareResult.validationErrors = await Promise.resolve(args.handleValidationErrorsShape(ve)); } else { diff --git a/packages/next-safe-action/src/validation-errors.ts b/packages/next-safe-action/src/validation-errors.ts index 2252cc18..130e3fd5 100644 --- a/packages/next-safe-action/src/validation-errors.ts +++ b/packages/next-safe-action/src/validation-errors.ts @@ -53,12 +53,10 @@ export const buildValidationErrors = (issues: Vali // This class is internally used to throw validation errors in action's server code function, using // `returnValidationErrors`. export class ActionServerValidationError extends Error { - public kind: string; public validationErrors: ValidationErrors; constructor(validationErrors: ValidationErrors) { super("Server Action server validation error(s) occurred"); this.validationErrors = validationErrors; - this.kind = "__actionServerValidationError"; } }