sveltejs / sveltejs/kit

async errors report the wrong location during SSR

Open
#10,779 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Describe the bug

I've been suffering from incorrect error locations for a long time and was very excited about #10769 and version 1.25.1.
However, it seems to be about server rendering and some IDE, and not related to errors in code executed in hooks.server.js.

Even in a stackblitz environment, the phenomenon is well reproduced. In the project I linked there is only a 2 line difference, but it is not consistent at all. Sometimes it is smaller than it actually is, but most of the time they report a larger number.

I keep the asynchronous infinite loop executed in hooks.server.js residing on the server like a daemon. And the values calculated by the infinite loop are frequently referenced and output by several pages of svelte. I don't think this is an overly unusual structure.

In that infinite loop, errors will occur frequently, and I want to quickly find their location. Currently I am having a very hard time.

Since I'm having a really hard time, I'd like to know at least a temporary solution.
For example, in #10769, I checked the following link: https://github.com/sveltejs/kit/blob/master/packages/kit/src/runtime/server/utils.js#L98
I found this to be of great help in making this temporary fix. I hope there is something similar for my case. I am not skilled enough to find it myself. Please help me.

Reproduction

https://stackblitz.com/edit/sveltejs-kit-template-default-wsaice?file=jsconfig.json

hooks.server.js:

console.log('run: hooks.server.js (line 1)');

setTimeout(() => {
	// Dummy code to place the error location in the middle of the file.  (line 4)

	console.log('error soon  (line 6)');
}, 3000);

setTimeout(() => {
	console.log('<--- line number 10', new Error().stack);
	throw new Error('<---- line number 11');
}, 4000);

setTimeout(() => {
	console.log('dummy'); //(line 15)

	console.log('dummy'); //(line 17)

	console.log('dummy'); //(line 19)
}, 10000);

package.json:

"@sveltejs/kit": "1.25.1"
Logs
run: hooks.server.js (line 1)
page option:  { prerender: false, ssr: false }
error soon  (line 6)
<--- line number 10 Error
    at Timeout.eval (/home/projects/sveltejs-kit-template-default-wsaice/src/hooks.server.js:12:37)
    at listOnTimeout (node:internal/timers:62:4448)
    at processTimers (node:internal/timers:62:5285)
    at <anonymous> (https://sveltejskittemplatedefaultwsai-dctb.w-credentialless.staticblitz.com/blitz.d6c42aca.js:334:292836)
    at <anonymous> (https://sveltejskittemplatedefaultwsai-dctb.w-credentialless.staticblitz.com/blitz.d6c42aca.js:334:293145)
Error: <---- line number 11
    at Timeout.eval (/home/projects/sveltejs-kit-template-default-wsaice/src/hooks.server.js:13:8)
    at listOnTimeout (node:internal/timers:62:4448)
    at processTimers (node:internal/timers:62:5285)
    at <anonymous> (https://sveltejskittemplatedefaultwsai-dctb.w-credentialless.staticblitz.com/blitz.d6c42aca.js:334:292836)
    at <anonymous> (https://sveltejskittemplatedefaultwsai-dctb.w-credentialless.staticblitz.com/blitz.d6c42aca.js:334:293145)
System Info
System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.20.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.4.2 - /usr/local/bin/npm
    pnpm: 8.6.10 - /usr/local/bin/pnpm
  npmPackages:
    @sveltejs/adapter-auto: ^2.0.0 => 2.1.0 
    @sveltejs/kit: 1.25.1 => 1.25.1 
    svelte: ^4.0.5 => 4.2.0 
    vite: ^4.4.2 => 4.4.9
Severity

annoyance

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 by reproducing the async error-location mismatch from the StackBlitz example and inspect the server-side execution of hooks.server.js. Compare the reported stack locations with the source lines shown in the reproduction; done means asynchronous SSR errors identify the correct source location.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.