Skip to content

Commit 0a0d67b

Browse files
committed
Fixed signature of App.Error.
1 parent bd401cf commit 0a0d67b

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- The [clearOnSubmit](https://superforms.rocks/concepts/submit-behavior#clearonsubmit) option didn't clear the errors when supposed to. To avoid a breaking change, **the default option for clearOnSubmit is now** `message`, not `errors-and-message`, as it didn't work anyway.
1313

14+
### Fixed
15+
16+
- the `event.error` signature in [onError](https://superforms.rocks/concepts/events#onerror) was incorrect, it doesn't always include `App.Error`. It is now a union between `App.Error` and its default SvelteKit signature, `{ message: string }`.
17+
1418
## [2.10.5] - 2024-03-18
1519

1620
### Added

src/lib/client/superForm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export type FormOptions<
126126
result: {
127127
type: 'error';
128128
status?: number;
129-
error: App.Error;
129+
error: App.Error | { message: string };
130130
};
131131
}) => MaybePromise<unknown | void>);
132132
onChange: (event: ChangeEvent<T>) => void;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { superValidate } from '$lib/index.js';
2+
import { zod } from '$lib/adapters/zod.js';
3+
import { userSchema } from './schema.js';
4+
import { error, fail } from '@sveltejs/kit';
5+
6+
export const load = async () => {
7+
const form = await superValidate({ name: 'some name' }, zod(userSchema));
8+
9+
return { form };
10+
};
11+
12+
export const actions = {
13+
default: async ({ request }) => {
14+
const form = await superValidate(request, zod(userSchema));
15+
console.dir(form, { depth: 10 }); //debug
16+
17+
if (!form.valid) {
18+
return fail(400, { form });
19+
}
20+
21+
if (form.data.exception) {
22+
throw new Error('test-error');
23+
} else {
24+
// @ts-expect-error Does not follow the App.Error shape
25+
error(502, { code: 'expected', title: 'Error title', message: 'Error' });
26+
}
27+
}
28+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<script lang="ts">
2+
import { defaults, superForm } from '$lib/index.js';
3+
import { userSchema } from './schema.js';
4+
import { zod } from '$lib/adapters/zod.js';
5+
6+
const formData = defaults({ name: 'First name' }, zod(userSchema));
7+
8+
let error: string;
9+
10+
export const spForm = superForm(formData, {
11+
onError: (e) => {
12+
console.log(e.result.error);
13+
// @ts-expect-error Does not follow the App.Error shape
14+
error = 'code' in e.result.error ? e.result.error.code : e.result.error.message;
15+
}
16+
});
17+
18+
const { enhance, form } = spForm;
19+
</script>
20+
21+
<p id="error">ERROR:{error}</p>
22+
23+
<form use:enhance method="POST">
24+
<input name="name" bind:value={$form.name} />
25+
<button type="submit">Save</button>
26+
<br /><input type="checkbox" name="exception" bind:checked={$form.exception} /> Throw exception
27+
</form>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { z } from 'zod';
2+
3+
export const userSchema = z.object({
4+
name: z.string(),
5+
exception: z.boolean()
6+
});

0 commit comments

Comments
 (0)