File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
src/content/docs/guides/forms Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -71,3 +71,38 @@ function formSubmit(){
71
71
</BindForm>
72
72
</Layout>
73
73
```
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
+ ```
You can’t perform that action at this time.
0 commit comments