feat: allow changing response http status code from handleError#14919
feat: allow changing response http status code from handleError#14919justinba1010 wants to merge 14 commits into
handleError#14919Conversation
🦋 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 <chewteeming01@gmail.com>
|
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 <chewteeming01@gmail.com>
Co-authored-by: Tee Ming <chewteeming01@gmail.com>
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.
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.
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
| * // Return 503 Service Unavailable for database errors | ||
| * if (error.message.includes('database')) { | ||
| * event.setStatusCode(503); | ||
| * } | ||
| * |
There was a problem hiding this comment.
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. |
|
Closing in favour of #16162 |
closes #14442
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.
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