Skip to content

Commit b2d5787

Browse files
authored
fix: ensure <svelte:boundary> properly removes error content in production mode (#15793)
* fix: ensure <svelte:boundary> properly removes error content in production mode * add changeset
1 parent 3d80884 commit b2d5787

File tree

5 files changed

+69
-44
lines changed

5 files changed

+69
-44
lines changed

Diff for: .changeset/eleven-roses-speak.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: ensure <svelte:boundary> properly removes error content in production mode

Diff for: packages/svelte/src/internal/client/runtime.js

+35-44
Original file line numberDiff line numberDiff line change
@@ -291,67 +291,58 @@ export function handle_error(error, effect, previous_effect, component_context)
291291
is_throwing_error = true;
292292
}
293293

294-
if (
295-
!DEV ||
296-
component_context === null ||
297-
!(error instanceof Error) ||
298-
handled_errors.has(error)
299-
) {
300-
propagate_error(error, effect);
301-
return;
302-
}
303-
304-
handled_errors.add(error);
294+
if (DEV && component_context !== null && error instanceof Error && !handled_errors.has(error)) {
295+
handled_errors.add(error);
305296

306-
const component_stack = [];
297+
const component_stack = [];
307298

308-
const effect_name = effect.fn?.name;
299+
const effect_name = effect.fn?.name;
309300

310-
if (effect_name) {
311-
component_stack.push(effect_name);
312-
}
301+
if (effect_name) {
302+
component_stack.push(effect_name);
303+
}
313304

314-
/** @type {ComponentContext | null} */
315-
let current_context = component_context;
305+
/** @type {ComponentContext | null} */
306+
let current_context = component_context;
316307

317-
while (current_context !== null) {
318-
if (DEV) {
308+
while (current_context !== null) {
319309
/** @type {string} */
320310
var filename = current_context.function?.[FILENAME];
321311

322312
if (filename) {
323313
const file = filename.split('/').pop();
324314
component_stack.push(file);
325315
}
316+
317+
current_context = current_context.p;
326318
}
327319

328-
current_context = current_context.p;
329-
}
320+
const indent = is_firefox ? ' ' : '\t';
321+
define_property(error, 'message', {
322+
value:
323+
error.message + `\n${component_stack.map((name) => `\n${indent}in ${name}`).join('')}\n`
324+
});
325+
define_property(error, 'component_stack', {
326+
value: component_stack
327+
});
330328

331-
const indent = is_firefox ? ' ' : '\t';
332-
define_property(error, 'message', {
333-
value: error.message + `\n${component_stack.map((name) => `\n${indent}in ${name}`).join('')}\n`
334-
});
335-
define_property(error, 'component_stack', {
336-
value: component_stack
337-
});
338-
339-
const stack = error.stack;
340-
341-
// Filter out internal files from callstack
342-
if (stack) {
343-
const lines = stack.split('\n');
344-
const new_lines = [];
345-
for (let i = 0; i < lines.length; i++) {
346-
const line = lines[i];
347-
if (line.includes('svelte/src/internal')) {
348-
continue;
329+
const stack = error.stack;
330+
331+
// Filter out internal files from callstack
332+
if (stack) {
333+
const lines = stack.split('\n');
334+
const new_lines = [];
335+
for (let i = 0; i < lines.length; i++) {
336+
const line = lines[i];
337+
if (line.includes('svelte/src/internal')) {
338+
continue;
339+
}
340+
new_lines.push(line);
349341
}
350-
new_lines.push(line);
342+
define_property(error, 'stack', {
343+
value: new_lines.join('\n')
344+
});
351345
}
352-
define_property(error, 'stack', {
353-
value: new_lines.join('\n')
354-
});
355346
}
356347

357348
propagate_error(error, effect);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script>
2+
throw new Error();
3+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
mode: ['client'],
6+
test({ assert, target }) {
7+
flushSync();
8+
9+
assert.htmlEqual(target.innerHTML, '<p>error occurred</p>');
10+
}
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script>
2+
import Child from "./Child.svelte"
3+
</script>
4+
5+
<svelte:boundary>
6+
<p>This should be removed</p>
7+
8+
{#if true}
9+
<Child />
10+
{/if}
11+
12+
{#snippet failed()}
13+
<p>error occurred</p>
14+
{/snippet}
15+
</svelte:boundary>

0 commit comments

Comments
 (0)