Skip to content
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

support for throwing error in handle function #7966

Closed
ivanhofer opened this issue Dec 6, 2022 · 2 comments · Fixed by #8829
Closed

support for throwing error in handle function #7966

ivanhofer opened this issue Dec 6, 2022 · 2 comments · Fixed by #8829
Labels
feature / enhancement New feature or request ready to implement please submit PRs for these issues!
Milestone

Comments

@ivanhofer
Copy link
Contributor

Describe the problem

When doing a client-side __data.json request, errors thrown with the error helper function inside hooks.server.ts , will not return the correct error object.

It works as expected when error gets thrown inside a +server.* file.

Here is a demonstration of the issue; https://stackblitz.com/edit/sveltejs-kit-template-default-djcepj?file=src/hooks.server.ts

  1. start the application
  2. click on the "test hooks.server.ts" link
  3. a 401 error gets thrown inside hooks.servert.ts
  4. the page shows a 500 error
  5. reload the page
  6. the 401 error gets correctly shown

related: #7609
The issue was already described as a sidenote there.

Describe the proposed solution

SvelteKit should support throwing error objects also for __data.json requests inside hooks.server.ts.
This makes the usage of the error helper function more consistent.

Alternatives considered

Leave it as it is

Importance

would make my life easier

Additional Information

No response

@theetrain
Copy link
Contributor

As a workaround, you can pass errors from your handle to your layout via locals. I've provided an example under 'alternatives' here: #7272 (comment)

@ivanhofer
Copy link
Contributor Author

Thanks, but this looks really hacky and error-prone. Then I would prefer to return a regular Response object

// this
throw error(401, { message: 'Unauthhorized' })
// would become
return Response(
	JSON.stringify({ message: 'Unauthorized' }),
	{
		status: 401,
		headers: {
			'content-type': 'application/json'
		}
	)
)

As you can see using the raw Response object makes you write additional boilerplate code. That's the reason why the error helper function was introduced by the SvelteKit team.

Adding support for throwing error inside handle makes it just more consistent with the rest of SvelteKit.

@Rich-Harris Rich-Harris added this to the whenever milestone Dec 9, 2022
@dummdidumm dummdidumm added ready to implement please submit PRs for these issues! and removed low hanging fruit labels Feb 1, 2023
dummdidumm added a commit that referenced this issue Feb 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature / enhancement New feature or request ready to implement please submit PRs for these issues!
Projects
None yet
4 participants