Skip to content

Commit a23599a

Browse files
authored
fix: don't show adjusted error messages in boundaries (#16360)
1 parent 4ef53a7 commit a23599a

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed

.changeset/large-balloons-agree.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: don't show adjusted error messages in boundaries

packages/svelte/src/internal/client/error-handling.js

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ import { BOUNDARY_EFFECT, EFFECT_RAN } from './constants.js';
77
import { define_property, get_descriptor } from '../shared/utils.js';
88
import { active_effect } from './runtime.js';
99

10+
const adjustments = new WeakMap();
11+
1012
/**
1113
* @param {unknown} error
1214
*/
1315
export function handle_error(error) {
1416
var effect = /** @type {Effect} */ (active_effect);
1517

16-
if (DEV && error instanceof Error) {
17-
adjust_error(error, effect);
18+
if (DEV && error instanceof Error && !adjustments.has(error)) {
19+
adjustments.set(error, get_adjustments(error, effect));
1820
}
1921

2022
if ((effect.f & EFFECT_RAN) === 0) {
@@ -48,21 +50,19 @@ export function invoke_error_boundary(error, effect) {
4850
effect = effect.parent;
4951
}
5052

53+
if (error instanceof Error) {
54+
apply_adjustments(error);
55+
}
56+
5157
throw error;
5258
}
5359

54-
/** @type {WeakSet<Error>} */
55-
const adjusted_errors = new WeakSet();
56-
5760
/**
5861
* Add useful information to the error message/stack in development
5962
* @param {Error} error
6063
* @param {Effect} effect
6164
*/
62-
function adjust_error(error, effect) {
63-
if (adjusted_errors.has(error)) return;
64-
adjusted_errors.add(error);
65-
65+
function get_adjustments(error, effect) {
6666
const message_descriptor = get_descriptor(error, 'message');
6767

6868
// if the message was already changed and it's not configurable we can't change it
@@ -78,17 +78,28 @@ function adjust_error(error, effect) {
7878
context = context.p;
7979
}
8080

81-
define_property(error, 'message', {
82-
value: error.message + `\n${component_stack}\n`
83-
});
81+
return {
82+
message: error.message + `\n${component_stack}\n`,
83+
stack: error.stack
84+
?.split('\n')
85+
.filter((line) => !line.includes('svelte/src/internal'))
86+
.join('\n')
87+
};
88+
}
89+
90+
/**
91+
* @param {Error} error
92+
*/
93+
function apply_adjustments(error) {
94+
const adjusted = adjustments.get(error);
95+
96+
if (adjusted) {
97+
define_property(error, 'message', {
98+
value: adjusted.message
99+
});
84100

85-
if (error.stack) {
86-
// Filter out internal modules
87101
define_property(error, 'stack', {
88-
value: error.stack
89-
.split('\n')
90-
.filter((line) => !line.includes('svelte/src/internal'))
91-
.join('\n')
102+
value: adjusted.stack
92103
});
93104
}
94105
}

packages/svelte/tests/runtime-runes/samples/error-boundary-3/_config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ export default test({
99
flushSync();
1010

1111
assert.deepEqual(logs, ['error caught']);
12-
assert.htmlEqual(target.innerHTML, `<div>Fallback!</div><button>+</button>`);
12+
assert.htmlEqual(target.innerHTML, `<div>oh no!</div><button>+</button>`);
1313
}
1414
});

packages/svelte/tests/runtime-runes/samples/error-boundary-3/main.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script>
22
function throw_error() {
3-
throw new Error('test')
3+
throw new Error('oh no!')
44
}
55
66
let count = $state(0);
@@ -9,8 +9,8 @@
99
<svelte:boundary onerror={(e) => console.log('error caught')}>
1010
{count > 0 ? throw_error() : null}
1111

12-
{#snippet failed()}
13-
<div>Fallback!</div>
12+
{#snippet failed(e)}
13+
<div>{e.message}</div>
1414
{/snippet}
1515
</svelte:boundary>
1616

0 commit comments

Comments
 (0)