sveltejs / sveltejs/kit

Set headers/cookies for error responses from hooks

Open
#9,188 5 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

error handling
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the problem

It's possible to end up in a state in an app where a cookie value of some kind is causing an error. For instance, you might have started an authentication request, only to have the request fail for some reason. If the request details are stored in a cookie, then the user might keep reloading the page and keep seeing an error page. We have this issue with our oauth-based login system.

We want to use handleError to handle these errors so we don't need to manually handle them all over the place, and instead can just throw when something goes wrong. But we need to clear the cookie which that error occurs, so the user gets back into a non-broken state.

Describe the proposed solution

Maybe handleError could return headers that get appended to the response?

export function handleError({ error, event }) {
  const errorId = crypto.randomUUID();
  // example integration with https://sentry.io/
  Sentry.captureException(error, { event, errorId });
 
  return {
    message: 'Whoops!',
    headers: {
      'Set-Cookie': '...',
    },
    errorId
  };
}
Alternatives considered

No response

Importance

would make my life easier

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the handleError hook and trace how errors become responses. Determine where headers from the hook could be incorporated, then verify that a returned Set-Cookie value reaches the error response and add coverage for the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.