Skip to content

Commit

Permalink
reset stack traces to avoid double fix - closes #3371
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Jul 20, 2022
1 parent a2158c8 commit 12983fe
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/plenty-radios-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Reset stack traces to avoid double-fix
9 changes: 8 additions & 1 deletion packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ export async function render_response({
/** @type {import('types').NormalizedLoadOutputCache | undefined} */
let cache;

if (error) {
const stack = error?.stack;

if (__SVELTEKIT_DEV__ && error) {
error.stack = options.get_stack(error);
}

Expand Down Expand Up @@ -315,6 +317,11 @@ export async function render_response({
}
}

if (__SVELTEKIT_DEV__ && error) {
// reset stack, otherwise it may be 'fixed' a second time
error.stack = stack;
}

return new Response(html, {
status,
headers
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const bad = foo().toUpperCase();
export default bad;

// @ts-expect-error
/** @returns {string} */
function foo() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
import bad from './_bad.js';
</script>

<h1>{bad}</h1>
13 changes: 13 additions & 0 deletions packages/kit/test/apps/basics/test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,19 @@ test.describe('Errors', () => {
expect(/** @type {Response} */ (response).status()).toBe(400);
}
});

test('stack traces are not fixed twice', async ({ page }) => {
await page.goto('/errors/stack-trace');
expect(await page.textContent('#message')).toBe(
'This is your custom error page saying: "Cannot read properties of undefined (reading \'toUpperCase\')"'
);

// check the stack wasn't mutated
await page.goto('/errors/stack-trace');
expect(await page.textContent('#message')).toBe(
'This is your custom error page saying: "Cannot read properties of undefined (reading \'toUpperCase\')"'
);
});
});

test.describe('Load', () => {
Expand Down

0 comments on commit 12983fe

Please sign in to comment.