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;