Skip to content

Commit

Permalink
feat: throw response
Browse files Browse the repository at this point in the history
  • Loading branch information
ido-pluto committed Mar 30, 2024
1 parent e3b49ce commit 5cd8057
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/content/docs/guides/forms/js-helpers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,38 @@ function formSubmit(){
</BindForm>
</Layout>
```

### Throw Response
If you want to throw a response from the server.

Meaning you in a nested function and you encore an error and you want to override an response and abort the current page execution.

You can use the `ThrowOverrideResponse` class.

```ts
new ThrowOverrideResponse(response?: Response, message?: string)
```

#### Example
```ts
import { ThrowOverrideResponse } from '@astro-utils/forms/forms.js';

throw new ThrowOverrideResponse(new Response('Unauthorized', { status: 401 }));
```

Edge case:
- If no `Response` is provided, will be use the response stored in `locals.forms.overrideResponse`.
- If no `Response` is stored in `locals.forms.overrideResponse`, will be return the message with error code 500.

#### Edge case example
```ts
import { ThrowOverrideResponse } from '@astro-utils/forms/forms.js';

Astro.locals.forms.redirect('/login');
throw new ThrowOverrideResponse();
```

Or you can just pass an error message:
```ts
throw new ThrowOverrideResponse(null, 'Unauthorized');
```

0 comments on commit 5cd8057

Please sign in to comment.