Skip to content

Commit 5cd8057

Browse files
committed
feat: throw response
1 parent e3b49ce commit 5cd8057

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/content/docs/guides/forms/js-helpers.mdx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,38 @@ function formSubmit(){
7171
</BindForm>
7272
</Layout>
7373
```
74+
75+
### Throw Response
76+
If you want to throw a response from the server.
77+
78+
Meaning you in a nested function and you encore an error and you want to override an response and abort the current page execution.
79+
80+
You can use the `ThrowOverrideResponse` class.
81+
82+
```ts
83+
new ThrowOverrideResponse(response?: Response, message?: string)
84+
```
85+
86+
#### Example
87+
```ts
88+
import { ThrowOverrideResponse } from '@astro-utils/forms/forms.js';
89+
90+
throw new ThrowOverrideResponse(new Response('Unauthorized', { status: 401 }));
91+
```
92+
93+
Edge case:
94+
- If no `Response` is provided, will be use the response stored in `locals.forms.overrideResponse`.
95+
- If no `Response` is stored in `locals.forms.overrideResponse`, will be return the message with error code 500.
96+
97+
#### Edge case example
98+
```ts
99+
import { ThrowOverrideResponse } from '@astro-utils/forms/forms.js';
100+
101+
Astro.locals.forms.redirect('/login');
102+
throw new ThrowOverrideResponse();
103+
```
104+
105+
Or you can just pass an error message:
106+
```ts
107+
throw new ThrowOverrideResponse(null, 'Unauthorized');
108+
```

0 commit comments

Comments
 (0)