-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: allow changing response http status code from handleError
#14919
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 8 commits
73dac27
d650e57
aa9ca9e
5964903
5c3a1f0
419662e
ea3ff07
dcea461
70d8532
78deb46
4a956e9
64d2917
9ca1978
e1a5954
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@sveltejs/kit': minor | ||
| --- | ||
|
|
||
| feat: allow returning a custom HTTP status code from the `handleError` hook |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -193,6 +193,14 @@ export async function internal_respond(request, options, manifest, state) { | |||||
| } | ||||||
| } | ||||||
| }, | ||||||
| setStatusCode: (code) => { | ||||||
| if (typeof code !== 'number' || code < 100 || code > 599) { | ||||||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How should we handle an invalid http code, it feels like throwing is wrong here, but implied behavior also doesn't feel terribly correct?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're not too far from what we're currently doing. See kit/packages/kit/src/exports/index.js Lines 74 to 75 in d3fe110
The only difference is the isNaN check and the error message format
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll add those tomorrow 👍 |
||||||
| throw new Error( | ||||||
| `Invalid status code: ${code}. Status code must be a number between 100 and 599` | ||||||
| ); | ||||||
| } | ||||||
| event_state.error_status_code = code; | ||||||
| }, | ||||||
| url, | ||||||
| isDataRequest: is_data_request, | ||||||
| isSubRequest: state.depth > 0, | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| /** | ||
| * This gets intercepted by the handleError hook and sets the status code to 422 | ||
| */ | ||
| export function GET() { | ||
| throw new Error('Custom error'); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1488,6 +1488,26 @@ declare module '@sveltejs/kit' { | |
| * You cannot add a `set-cookie` header with `setHeaders` — use the [`cookies`](https://svelte.dev/docs/kit/@sveltejs-kit#Cookies) API instead. | ||
| */ | ||
| setHeaders: (headers: Record<string, string>) => void; | ||
| /** | ||
| * Override the status code for error responses. This is useful when you want to customize the HTTP status code returned for an error, for example: | ||
| * | ||
| * ```js | ||
| * /// file: src/hooks.server.js | ||
| * export async function handleError({ error, event, status, message }) { | ||
| * // Return 503 Service Unavailable for database errors | ||
| * if (error.message.includes('database')) { | ||
| * event.setStatusCode(503); | ||
| * } | ||
| * | ||
|
Comment on lines
+1497
to
+1501
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't want to be too opinionated for an example, very open to suggestions, I'd feel partial to the convention we're using for our internal exceptions if you'd like as it's one common in monorepos. |
||
| * return { | ||
| * message: 'An error occurred' | ||
| * }; | ||
| * } | ||
| * ``` | ||
| * | ||
| * This method can only be called from the `handleError` hook and only affects error responses. | ||
| */ | ||
| setStatusCode: (code: number) => void; | ||
| /** | ||
| * The requested URL. | ||
| */ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.