-
-
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?
Conversation
🦋 Changeset detectedLatest commit: e1a5954 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This comment was marked as duplicate.
This comment was marked as duplicate.
handleError
Co-authored-by: Tee Ming <[email protected]>
|
Thanks @teemingc! Great suggestions, I've applied those diffs |
|
PR looks good to me but I think we might have some reservations about whether this is the best solution or if it's better to avoid ending up in the |
Co-authored-by: Tee Ming <[email protected]>
Co-authored-by: Tee Ming <[email protected]>
Sounds good, thank you guys! Commited the suggestions just now as well. |
|
How about passing a |
| } | ||
| }, | ||
| setStatusCode: (code) => { | ||
| if (typeof code !== 'number' || code < 100 || code > 599) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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
| if ((!BROWSER || DEV) && (isNaN(status) || status < 400 || status > 599)) { | |
| throw new Error(`HTTP error status codes must be between 400 and 599 — ${status} is invalid`); |
The only difference is the
isNaN check and the error message format
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add those tomorrow 👍
| * // Return 503 Service Unavailable for database errors | ||
| * if (error.message.includes('database')) { | ||
| * event.setStatusCode(503); | ||
| * } | ||
| * |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.
This one might be a bit of a footgun because it leads to situations where more than one statement is setting the HTTP status code in the same flow of execution. For example, in a server endpoint: export function GET({ setStatusCode }) {
setStatusCode(501);
return new Response(null, { status: 201 });
}If we do want to make it a function, it might be better to scope it to the |
Ahh that's a really good point. If you can give me some pointers on how that should look, I can get started on that tomorrow, had some difficulty implementing there at first. |
If it's scoped to the server I would still hold off on this until we can get more eyes on whether this is what we want going forward. The team is currently busy with Svelte's async feature. |
Allow Custom HTTP Status Codes From Error Handling
Allows custom http status codes from error objects allow for more robust error handling for production SvelteKit users. This is useful if you are using custom error handling, we allow errors to propagate from other services outside of SvelteKit.
Issue #14442
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.Edits