|
6 | 6 | import { schema } from './schema.js'; |
7 | 7 | import { debounce } from 'throttle-debounce'; |
8 | 8 |
|
| 9 | + let isSuccess = true; |
| 10 | + let shouldThrowIfNotSuccess = true; |
| 11 | + let defaultStatus = false; |
| 12 | +
|
9 | 13 | const { form, errors, message, enhance, submit, submitting } = superForm( |
10 | 14 | defaults({ email: '[email protected]', name: 'aaa' }, zod(schema)), |
11 | 15 | { |
|
20 | 24 | errors: Record<string, string[]>; |
21 | 25 | } & Record<string, unknown>; |
22 | 26 |
|
23 | | - result.status = error.status; |
| 27 | + if (!defaultStatus) result.status = error.status; |
24 | 28 | form.errors = error.errors; |
25 | 29 | }, |
26 | 30 | async onUpdate({ form, result }) { |
|
31 | 35 | return; |
32 | 36 | } |
33 | 37 |
|
34 | | - const isSuccess = false; //Math.random() >= 0.5; |
35 | 38 | console.log('isSuccess', isSuccess); |
36 | 39 |
|
37 | 40 | if (isSuccess) { |
|
54 | 57 | } |
55 | 58 | }; |
56 | 59 |
|
57 | | - const shouldThrow = false; //Math.random() >= 0.5; |
58 | | - console.log('shouldThrow', shouldThrow); |
| 60 | + console.log('shouldThrow', shouldThrowIfNotSuccess); |
59 | 61 |
|
60 | | - if (shouldThrow) { |
| 62 | + if (shouldThrowIfNotSuccess) { |
61 | 63 | // 3. This will update the status in the next release: |
62 | 64 | throw madeupProblemDetails; |
63 | 65 | } |
64 | 66 |
|
65 | 67 | setError(form, 'name', ['random error']); |
66 | | - result.status = 422; |
| 68 | + if (!defaultStatus) result.status = 423; |
67 | 69 | result.type = 'failure'; |
68 | 70 | // 3. Cannot set data to anything else than a SuperValidated object. In general, tamper as little as possible with the result |
69 | 71 | // 4. Focus will be kept when this is commented out. |
|
83 | 85 |
|
84 | 86 | <h3>onError testing</h3> |
85 | 87 |
|
| 88 | +<div> |
| 89 | + <input type="checkbox" bind:checked={isSuccess} /> Succeed |
| 90 | + <br /> |
| 91 | + <input type="checkbox" bind:checked={shouldThrowIfNotSuccess} /> Throw if not success |
| 92 | + <br /> |
| 93 | + <input type="checkbox" bind:checked={defaultStatus} /> Status 500 on error |
| 94 | +</div> |
| 95 | + |
86 | 96 | {#if $message} |
87 | 97 | <pre |
88 | 98 | class="status" |
|
0 commit comments