Skip to content

Commit 92b2686

Browse files
authored
feat: add action failure helper (#12878)
closes #12611 Exposes a helper method to check if a variable is an instanceof ActionFailure
1 parent fc27361 commit 92b2686

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

.changeset/fresh-guests-knock.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': minor
3+
---
4+
5+
feat: add helper to identify `ActionFailure` objects

packages/kit/src/exports/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,12 @@ export function fail(status, data) {
190190
// @ts-expect-error unique symbol missing
191191
return new ActionFailure(status, data);
192192
}
193+
194+
/**
195+
* Checks whether this is an action failure thrown by {@link fail}.
196+
* @param {unknown} e The object to check.
197+
* @return {e is import('./public.js').ActionFailure}
198+
*/
199+
export function isActionFailure(e) {
200+
return e instanceof ActionFailure;
201+
}

packages/kit/types/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,6 +1826,11 @@ declare module '@sveltejs/kit' {
18261826
* @param data Data associated with the failure (e.g. validation errors)
18271827
* */
18281828
export function fail<T extends Record<string, unknown> | undefined = undefined>(status: number, data: T): ActionFailure<T>;
1829+
/**
1830+
* Checks whether this is an action failure thrown by {@link fail}.
1831+
* @param e The object to check.
1832+
* */
1833+
export function isActionFailure(e: unknown): e is ActionFailure<undefined>;
18291834
export type LessThan<TNumber extends number, TArray extends any[] = []> = TNumber extends TArray['length'] ? TArray[number] : LessThan<TNumber, [...TArray, TArray['length']]>;
18301835
export type NumericRange<TStart extends number, TEnd extends number> = Exclude<TEnd | LessThan<TEnd>, LessThan<TStart>>;
18311836
export const VERSION: string;

0 commit comments

Comments
 (0)