Skip to content

fix: ensure <svelte:boundary> properly removes error content in production mode #15793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eleven-roses-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure <svelte:boundary> properly removes error content in production mode
79 changes: 35 additions & 44 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,67 +291,58 @@ export function handle_error(error, effect, previous_effect, component_context)
is_throwing_error = true;
}

if (
!DEV ||
component_context === null ||
!(error instanceof Error) ||
handled_errors.has(error)
) {
propagate_error(error, effect);
return;
}

handled_errors.add(error);
if (DEV && component_context !== null && error instanceof Error && !handled_errors.has(error)) {
handled_errors.add(error);

const component_stack = [];
const component_stack = [];

const effect_name = effect.fn?.name;
const effect_name = effect.fn?.name;

if (effect_name) {
component_stack.push(effect_name);
}
if (effect_name) {
component_stack.push(effect_name);
}

/** @type {ComponentContext | null} */
let current_context = component_context;
/** @type {ComponentContext | null} */
let current_context = component_context;

while (current_context !== null) {
if (DEV) {
while (current_context !== null) {
/** @type {string} */
var filename = current_context.function?.[FILENAME];

if (filename) {
const file = filename.split('/').pop();
component_stack.push(file);
}

current_context = current_context.p;
}

current_context = current_context.p;
}
const indent = is_firefox ? ' ' : '\t';
define_property(error, 'message', {
value:
error.message + `\n${component_stack.map((name) => `\n${indent}in ${name}`).join('')}\n`
});
define_property(error, 'component_stack', {
value: component_stack
});

const indent = is_firefox ? ' ' : '\t';
define_property(error, 'message', {
value: error.message + `\n${component_stack.map((name) => `\n${indent}in ${name}`).join('')}\n`
});
define_property(error, 'component_stack', {
value: component_stack
});

const stack = error.stack;

// Filter out internal files from callstack
if (stack) {
const lines = stack.split('\n');
const new_lines = [];
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line.includes('svelte/src/internal')) {
continue;
const stack = error.stack;

// Filter out internal files from callstack
if (stack) {
const lines = stack.split('\n');
const new_lines = [];
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line.includes('svelte/src/internal')) {
continue;
}
new_lines.push(line);
}
new_lines.push(line);
define_property(error, 'stack', {
value: new_lines.join('\n')
});
}
define_property(error, 'stack', {
value: new_lines.join('\n')
});
}

propagate_error(error, effect);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script>
throw new Error();
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
mode: ['client'],
test({ assert, target }) {
flushSync();

assert.htmlEqual(target.innerHTML, '<p>error occurred</p>');
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script>
import Child from "./Child.svelte"
</script>

<svelte:boundary>
<p>This should be removed</p>

{#if true}
<Child />
{/if}

{#snippet failed()}
<p>error occurred</p>
{/snippet}
</svelte:boundary>