Skip to content

Commit

Permalink
feat: add action failure helper (#12878)
Browse files Browse the repository at this point in the history
closes #12611

Exposes a helper method to check if a variable is an instanceof ActionFailure
  • Loading branch information
eltigerchino authored Nov 5, 2024
1 parent fc27361 commit 92b2686
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-guests-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': minor
---

feat: add helper to identify `ActionFailure` objects
9 changes: 9 additions & 0 deletions packages/kit/src/exports/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 5 additions & 0 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,11 @@ declare module '@sveltejs/kit' {
* @param data Data associated with the failure (e.g. validation errors)
* */
export function fail<T extends Record<string, unknown> | undefined = undefined>(status: number, data: T): ActionFailure<T>;
/**
* 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<undefined>;
export type LessThan<TNumber extends number, TArray extends any[] = []> = TNumber extends TArray['length'] ? TArray[number] : LessThan<TNumber, [...TArray, TArray['length']]>;
export type NumericRange<TStart extends number, TEnd extends number> = Exclude<TEnd | LessThan<TEnd>, LessThan<TStart>>;
export const VERSION: string;
Expand Down

0 comments on commit 92b2686

Please sign in to comment.