Skip to content

support for throwing redirect in handle function #7609

@ivanhofer

Description

@ivanhofer

Describe the problem

The convenient function redirect is really useful to redirect users to another page. It can be used in a lot of different files files, except for the hooks file.

It is not that unlikely to have a logic inside the handle function that checks if the user is authorized and then redirect the user to the login page.

In order to do so someone can't throw a redirect as everything thrown inside handle will result in an 500 error. In order to redirect a proper Response object needs to be returned.

if (!authorized) {
	return new Response(null, { status: 302, headers: { Location: '/login' } })
}

Writing it like this involves writing a bit of an overhead, but I would really love to use the redirect helper function like I do in all other code parts.

I'm currently using a wrapper to handle this use case where handleRequest is the actual implementation of my handle functionality:

export const handle: Handle = async (input) => handleRequest(input)
	.catch(error => {
		// can't use instanceof `Redirect` because class get's not exported
		if (error.status && error.location) {
			return new Response(null, {
				status: error.status,
				headers: { location: error.location }
			})
		}

		// can't use instanceof `HttpError` because class get's not exported
		if (error.status && error.body) {
			return new Response(JSON.stringify(error.body), { status: error.status })
		}

		return new Response(null, { status: 500 })
	})

Here I'm catching Redirects and HttpErrors and implementing and returning custom Responses. But this has a few weak points:

  • user code does not have access to Redirect, so I have to check it in another way
  • when SvelteKit changes the implementation of the Redirect class, my code will no longer work
  • possible inconsistencies with framework code (I did not take a look what exactly happens when SvelteKit handles `Redirect objects)
  • does not handle setting cookies (related to cookies are not being set when returning custom Response objects in handle #7611)

It would be great to have an official solution to this problem.

Describe the proposed solution

SvelteKit should catch errors from the handle function and redirect the user if a Redirect class gets thrown.

A similar thing could be done for the HttpError class thrown by the error() helper function. I guess that is related to this issue: #7272.
At least SvelteKit could set the correct status code when someone uses the error convenient function to throw inside handle.

Alternatives considered

Leave it to users to

  1. discover that this functionality it is not supported
  2. figure out a way to make redirects happen

Importance

would make my life easier

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions