Skip to content

ref(sveltekit): Improve SvelteKit 2.0 Not Found server error handling #9901

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

Merged
merged 5 commits into from
Dec 19, 2023

Conversation

Lms24
Copy link
Member

@Lms24 Lms24 commented Dec 18, 2023

This PR improves our logic to filter out "Not Found" errors in our server-side handleError hook wrapper:

  • We now use SvelteKit 2.0 - native error properties (status and message) to check for "Not Found" errors
  • Adjusted types for type safety and backwards compatibility with SvelteKit 1.x where the two properties property don't exist.
  • Updated Sveltekit to 2.0 in our dev dependency to work with latest types.

Not really happy with the two @ts-expect-error but I didn't find anything that looks nicer and still guarantees type safety when we work with the input object. Definitely open to ideas!

ref #9851

@Lms24 Lms24 changed the title ref(sveltekit): Improve SvelteKit 2.0 Not Found error handling ref(sveltekit): Improve SvelteKit 2.0 Not Found server error handling Dec 18, 2023
@Lms24 Lms24 force-pushed the lms/ref-sveltekit-improve-server-handleError branch from 87f2c17 to 8cfeb81 Compare December 18, 2023 17:43
Comment on lines +14 to +15
// TODO: add backwards-compatible type for kit 1.x (soon)
type HandleClientErrorInput = Parameters<HandleClientError>[0];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will add this in a follow-up PR where I'll add filtering out "Not Found" errors on the client side

@@ -34,7 +34,7 @@ describe('isHttpError', () => {
expect(isHttpError(httpErrorObject)).toBe(true);
});

it.each([new Error(), redirect(301, '/users/id'), 'string error', { status: 404 }, { body: 'Not found' }])(
it.each([new Error(), { status: 301, message: '/users/id' }, 'string error', { status: 404 }, { body: 'Not found' }])(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

calling redirect throws since Sveltekit 2.0, so I rewrote this test to what redirect used to return in kit 1.x (this is not breaking or relevant for kit 2.0)

@Lms24 Lms24 requested review from AbhiPrasad and lforst December 18, 2023 17:51
@Lms24 Lms24 marked this pull request as ready for review December 18, 2023 17:51
@Lms24 Lms24 mentioned this pull request Dec 18, 2023
@Lms24 Lms24 self-assigned this Dec 18, 2023
const { error, event, status, message } = input;

// SvelteKit 2.0 offers a reliable way to check for a Not Found error:
if (status === 404 && message === 'Not Found') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: is it just fine to only check status code?

Copy link
Member Author

@Lms24 Lms24 Dec 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the check for both, message and status because they're not determined identically:

https://github.com/sveltejs/kit/blob/fc8389c9cf42a02665edd8aa7a6840fe982134c9/packages/kit/src/utils/error.js#L26-L39

So while I think it doesn't happen at the moment, a HttpError could be thrown with a 404 status code and another message. If we only check for 404, we'd also filter out that error - whatever it is. Are we fine with that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would assume any 404 errors are not useful, but perhaps it's good to be conservative on what we filter for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not report 404s as errors. Filtering by transaction/span status should be powerful enough for users to achieve what they want.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the check to only consider the 404 status. You're right - we shouldn't capture other 404s either.

const { error, event, status, message } = input;

// SvelteKit 2.0 offers a reliable way to check for a Not Found error:
if (status === 404 && message === 'Not Found') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not report 404s as errors. Filtering by transaction/span status should be powerful enough for users to achieve what they want.

@Lms24 Lms24 force-pushed the lms/ref-sveltekit-improve-server-handleError branch from a4e6a66 to 51788bc Compare December 19, 2023 12:18
@Lms24 Lms24 enabled auto-merge (squash) December 19, 2023 12:20
@Lms24 Lms24 merged commit cef3621 into develop Dec 19, 2023
@Lms24 Lms24 deleted the lms/ref-sveltekit-improve-server-handleError branch December 19, 2023 12:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants