Skip to content

Commit 12983fe

Browse files
committed
reset stack traces to avoid double fix - closes #3371
1 parent a2158c8 commit 12983fe

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

.changeset/plenty-radios-turn.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Reset stack traces to avoid double-fix

packages/kit/src/runtime/server/page/render.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ export async function render_response({
7272
/** @type {import('types').NormalizedLoadOutputCache | undefined} */
7373
let cache;
7474

75-
if (error) {
75+
const stack = error?.stack;
76+
77+
if (__SVELTEKIT_DEV__ && error) {
7678
error.stack = options.get_stack(error);
7779
}
7880

@@ -315,6 +317,11 @@ export async function render_response({
315317
}
316318
}
317319

320+
if (__SVELTEKIT_DEV__ && error) {
321+
// reset stack, otherwise it may be 'fixed' a second time
322+
error.stack = stack;
323+
}
324+
318325
return new Response(html, {
319326
status,
320327
headers
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const bad = foo().toUpperCase();
2+
export default bad;
3+
4+
// @ts-expect-error
5+
/** @returns {string} */
6+
function foo() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import bad from './_bad.js';
3+
</script>
4+
5+
<h1>{bad}</h1>

packages/kit/test/apps/basics/test/server.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,19 @@ test.describe('Errors', () => {
380380
expect(/** @type {Response} */ (response).status()).toBe(400);
381381
}
382382
});
383+
384+
test('stack traces are not fixed twice', async ({ page }) => {
385+
await page.goto('/errors/stack-trace');
386+
expect(await page.textContent('#message')).toBe(
387+
'This is your custom error page saying: "Cannot read properties of undefined (reading \'toUpperCase\')"'
388+
);
389+
390+
// check the stack wasn't mutated
391+
await page.goto('/errors/stack-trace');
392+
expect(await page.textContent('#message')).toBe(
393+
'This is your custom error page saying: "Cannot read properties of undefined (reading \'toUpperCase\')"'
394+
);
395+
});
383396
});
384397

385398
test.describe('Load', () => {

0 commit comments

Comments
 (0)