From 92b2686314a7dbebee1761c3da7719d599f003c7 Mon Sep 17 00:00:00 2001 From: Tee Ming Date: Wed, 6 Nov 2024 03:30:45 +0800 Subject: [PATCH] feat: add action failure helper (#12878) closes #12611 Exposes a helper method to check if a variable is an instanceof ActionFailure --- .changeset/fresh-guests-knock.md | 5 +++++ packages/kit/src/exports/index.js | 9 +++++++++ packages/kit/types/index.d.ts | 5 +++++ 3 files changed, 19 insertions(+) create mode 100644 .changeset/fresh-guests-knock.md diff --git a/.changeset/fresh-guests-knock.md b/.changeset/fresh-guests-knock.md new file mode 100644 index 000000000000..19db1582ae4e --- /dev/null +++ b/.changeset/fresh-guests-knock.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': minor +--- + +feat: add helper to identify `ActionFailure` objects diff --git a/packages/kit/src/exports/index.js b/packages/kit/src/exports/index.js index 32fb827dc629..5454a2e15e31 100644 --- a/packages/kit/src/exports/index.js +++ b/packages/kit/src/exports/index.js @@ -190,3 +190,12 @@ export function fail(status, data) { // @ts-expect-error unique symbol missing return new ActionFailure(status, data); } + +/** + * Checks whether this is an action failure thrown by {@link fail}. + * @param {unknown} e The object to check. + * @return {e is import('./public.js').ActionFailure} + */ +export function isActionFailure(e) { + return e instanceof ActionFailure; +} diff --git a/packages/kit/types/index.d.ts b/packages/kit/types/index.d.ts index c943a2bc7179..237b3e3ff57e 100644 --- a/packages/kit/types/index.d.ts +++ b/packages/kit/types/index.d.ts @@ -1826,6 +1826,11 @@ declare module '@sveltejs/kit' { * @param data Data associated with the failure (e.g. validation errors) * */ export function fail | undefined = undefined>(status: number, data: T): ActionFailure; + /** + * Checks whether this is an action failure thrown by {@link fail}. + * @param e The object to check. + * */ + export function isActionFailure(e: unknown): e is ActionFailure; export type LessThan = TNumber extends TArray['length'] ? TArray[number] : LessThan; export type NumericRange = Exclude, LessThan>; export const VERSION: string;