Skip to content

Returning a bad "redirect" from svelte module load does not trigger handleError hook #3388

@moatra

Description

@moatra

Describe the bug

If the load function of a page module returns an object with the redirect property set but the status property is undefined, sveltekit generates an error to the dev to let them know the mistake. However, this error does not flow into the handleError hook where the dev is likely to have set up error logging.

Reproduction

  1. Clone https://github.com/moatra/sveltekit-redirect-error-handler
  2. npm install
  3. npm run dev
  4. Load http://localhost:3000/err_gen in a new browser tab.
  • Observe the logs (first block in Logs). The error appears in both handleError and the __error.svelte component.
  1. Load http://localhost:3000/bad_redirect in a new browser tab (or otherwise force SSR)
  • Observe the logs (second block in Logs). The error only appers in the __error.svelte component.

Logs

Visiting /err_gen:

handleError() called for /err_gen Error: throwing an error from err_gen.svelte
    at load (err_gen.svelte:3:18)
    at load_node (file:///home/thomas/Projects/sveltekit-redirect-error-handler/node_modules/@sveltejs/kit/dist/chunks/index.js:1133:30)
    at respond$1 (file:///home/thomas/Projects/sveltekit-redirect-error-handler/node_modules/@sveltejs/kit/dist/chunks/index.js:1347:21)
    at async render_page (file:///home/thomas/Projects/sveltekit-redirect-error-handler/node_modules/@sveltejs/kit/dist/chunks/index.js:1538:19)
    at async resolve (file:///home/thomas/Projects/sveltekit-redirect-error-handler/node_modules/@sveltejs/kit/dist/chunks/index.js:1863:10)
    at async Object.handle (//home/thomas/Projects/sveltekit-redirect-error-handler/src/hooks:8:19)
    at async respond (file:///home/thomas/Projects/sveltekit-redirect-error-handler/node_modules/@sveltejs/kit/dist/chunks/index.js:1829:10)
    at async file:///home/thomas/Projects/sveltekit-redirect-error-handler/node_modules/@sveltejs/kit/dist/chunks/index.js:2157:24
The __error.svelte component saw an error: Error: throwing an error from err_gen.svelte
    at load (err_gen.svelte:3:18)
    at load_node (file:///home/thomas/Projects/sveltekit-redirect-error-handler/node_modules/@sveltejs/kit/dist/chunks/index.js:1133:30)
    at respond$1 (file:///home/thomas/Projects/sveltekit-redirect-error-handler/node_modules/@sveltejs/kit/dist/chunks/index.js:1347:21)
    at async render_page (file:///home/thomas/Projects/sveltekit-redirect-error-handler/node_modules/@sveltejs/kit/dist/chunks/index.js:1538:19)
    at async resolve (file:///home/thomas/Projects/sveltekit-redirect-error-handler/node_modules/@sveltejs/kit/dist/chunks/index.js:1863:10)
    at async Object.handle (//home/thomas/Projects/sveltekit-redirect-error-handler/src/hooks:8:19)
    at async respond (file:///home/thomas/Projects/sveltekit-redirect-error-handler/node_modules/@sveltejs/kit/dist/chunks/index.js:1829:10)
    at async file:///home/thomas/Projects/sveltekit-redirect-error-handler/node_modules/@sveltejs/kit/dist/chunks/index.js:2157:24

Visiting /bad_redirect:

The __error.svelte component saw an error: Error: "redirect" property returned from load() must be accompanied by a 3xx status code
    at normalize (file:///home/thomas/Projects/sveltekit-redirect-error-handler/node_modules/@sveltejs/kit/dist/chunks/index.js:832:12)
    at load_node (file:///home/thomas/Projects/sveltekit-redirect-error-handler/node_modules/@sveltejs/kit/dist/chunks/index.js:1148:11)
    at async respond$1 (file:///home/thomas/Projects/sveltekit-redirect-error-handler/node_modules/@sveltejs/kit/dist/chunks/index.js:1347:15)
    at async render_page (file:///home/thomas/Projects/sveltekit-redirect-error-handler/node_modules/@sveltejs/kit/dist/chunks/index.js:1538:19)
    at async resolve (file:///home/thomas/Projects/sveltekit-redirect-error-handler/node_modules/@sveltejs/kit/dist/chunks/index.js:1863:10)
    at async Object.handle (//home/thomas/Projects/sveltekit-redirect-error-handler/src/hooks.js:12:19)
    at async respond (file:///home/thomas/Projects/sveltekit-redirect-error-handler/node_modules/@sveltejs/kit/dist/chunks/index.js:1829:10)
    at async file:///home/thomas/Projects/sveltekit-redirect-error-handler/node_modules/@sveltejs/kit/dist/chunks/index.js:2157:24

System Info

❯ npx envinfo --system --binaries --browsers --npmPackages "{svelte,@sveltejs/*,vite}"

  System:
    OS: Linux 5.15 Fedora Linux 35 (Workstation Edition)
    CPU: (16) x64 AMD Ryzen 7 3700X 8-Core Processor
    Memory: 23.68 GB / 31.33 GB
    Container: Yes
    Shell: 5.1.8 - /bin/bash
  Binaries:
    Node: 16.13.0 - /usr/bin/node
    npm: 8.1.0 - /usr/bin/npm
  Browsers:
    Firefox: 96.0
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.11 
    @sveltejs/kit: next => 1.0.0-next.232 
    svelte: ^3.44.0 => 3.46.2

Severity

annoyance

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions