Skip to content

Commit 1fc9912

Browse files
committed
Error status for event exceptions fixed.
1 parent abcd34c commit 1fc9912

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

src/lib/client/superForm.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@ export type FormOptions<
9191
selectErrorText: boolean;
9292
stickyNavbar: string;
9393
taintedMessage: string | boolean | null | (() => MaybePromise<boolean>);
94-
SPA: true | { failStatus?: number } | string;
94+
/**
95+
* Enable single page application (SPA) mode.
96+
* **The string and failStatus options are deprecated** and will be removed in the next major release.
97+
* @see https://superforms.rocks/concepts/spa
98+
*/
99+
SPA: true | /** @deprecated */ string | /** @deprecated */ { failStatus?: number };
95100

96101
onSubmit: (
97102
input: Parameters<SubmitFunction>[0] & {
@@ -657,13 +662,12 @@ export function superForm<
657662
return options.SPA === true || typeof options.SPA === 'object';
658663
}
659664

660-
function Form_errorStatus(defaultStatus?: number) {
665+
function Form_resultStatus(defaultStatus: number) {
666+
if (defaultStatus >= 500) return defaultStatus;
661667
return (
662-
defaultStatus ||
663668
(typeof options.SPA === 'boolean' || typeof options.SPA === 'string'
664669
? undefined
665-
: options.SPA?.failStatus) ||
666-
500
670+
: options.SPA?.failStatus) || defaultStatus
667671
);
668672
}
669673

@@ -1640,7 +1644,7 @@ export function superForm<
16401644
function clientValidationResult(validation: SuperFormValidated<T, M, In>) {
16411645
const validationResult = { ...validation, posted: true };
16421646

1643-
const status = validationResult.valid ? 200 : Form_errorStatus();
1647+
const status = validationResult.valid ? 200 : Form_resultStatus(400);
16441648

16451649
const data = { form: validationResult };
16461650

@@ -1679,7 +1683,7 @@ export function superForm<
16791683
// picked up in page.subscribe in superForm.
16801684
const failResult = {
16811685
type: 'failure',
1682-
status: Form_errorStatus(Math.floor(result.status ?? NaN)),
1686+
status: Form_resultStatus(Math.floor(result.status ?? 400)),
16831687
data: result
16841688
} as const;
16851689
await applyAction(failResult);
@@ -1736,7 +1740,7 @@ export function superForm<
17361740
await event(submit);
17371741
} catch (error) {
17381742
cancel();
1739-
triggerOnError({ type: 'error', error, status: Form_errorStatus() });
1743+
triggerOnError({ type: 'error', error, status: Form_resultStatus(500) });
17401744
}
17411745
}
17421746
}
@@ -1869,7 +1873,7 @@ export function superForm<
18691873
? (event.result as ActionResult)
18701874
: {
18711875
type: 'error',
1872-
status: Form_errorStatus(parseInt(String(event.result.status))),
1876+
status: Form_resultStatus(parseInt(String(event.result.status)) || 500),
18731877
error: event.result.error instanceof Error ? event.result.error : event.result
18741878
};
18751879

@@ -1895,7 +1899,7 @@ export function superForm<
18951899
data.result = {
18961900
type: 'error',
18971901
error,
1898-
status: result.status && result.status >= 400 ? result.status : Form_errorStatus()
1902+
status: Form_resultStatus(Math.max(result.status ?? 500, 500))
18991903
};
19001904
}
19011905

src/routes/(v2)/v2/spa-error/+page.svelte

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,26 @@
1010
const adapter = arktype(schema, { defaults: defaultData });
1111
const data = defaults(adapter);
1212
13-
let options = ['onSubmit', 'onResult', 'onUpdate'];
14-
let option = 'onSubmit';
13+
let options = ['onSubmit', 'onResult', 'onUpdate'] as const;
14+
let option: (typeof options)[number] = 'onSubmit';
1515
1616
let error: Extract<ActionResult, { type: 'error' }>;
1717
18-
const {
19-
form,
20-
message,
21-
enhance,
22-
submitting,
23-
options: formOptions
24-
} = superForm(data, {
18+
const { form, message, enhance, submitting } = superForm(data, {
2519
SPA: true,
2620
validators: arktype(schema, { defaults: defaultData }),
2721
async onSubmit() {
28-
formOptions.SPA = true;
2922
if (option == 'onSubmit') {
3023
throw new Error('onSubmit error');
3124
}
3225
},
3326
async onResult() {
3427
if (option == 'onResult') {
35-
formOptions.SPA = { failStatus: 501 };
3628
throw new Error('onResult error');
3729
}
3830
},
3931
async onUpdate() {
4032
if (option == 'onUpdate') {
41-
formOptions.SPA = { failStatus: 502 };
4233
throw new Error('onUpdate error');
4334
}
4435
},

0 commit comments

Comments
 (0)