Skip to content

Commit 6c2064b

Browse files
committed
only apply error adjustments when error escapes boundaries
1 parent c79553b commit 6c2064b

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

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

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { ERROR_VALUE, BOUNDARY_EFFECT, EFFECT_RAN } from './constants.js';
77
import { define_property, get_descriptor } from '../shared/utils.js';
88
import { active_effect, active_reaction } from './runtime.js';
99

10+
const adjustments = new WeakMap();
11+
1012
/**
1113
* @param {unknown} error
1214
*/
@@ -19,14 +21,18 @@ export function handle_error(error) {
1921
return error;
2022
}
2123

22-
if (DEV && error instanceof Error) {
23-
// adjust_error(error, effect);
24+
if (DEV && error instanceof Error && !adjustments.has(error)) {
25+
adjustments.set(error, get_adjustments(error, effect));
2426
}
2527

2628
if ((effect.f & EFFECT_RAN) === 0) {
2729
// if the error occurred while creating this subtree, we let it
2830
// bubble up until it hits a boundary that can handle it
2931
if ((effect.f & BOUNDARY_EFFECT) === 0) {
32+
if (!effect.parent && error instanceof Error) {
33+
apply_adjustments(error);
34+
}
35+
3036
throw error;
3137
}
3238

@@ -53,6 +59,10 @@ export function invoke_error_boundary(error, effect) {
5359
effect = effect.parent;
5460
}
5561

62+
if (error instanceof Error) {
63+
apply_adjustments(error);
64+
}
65+
5666
throw error;
5767
}
5868

@@ -64,7 +74,7 @@ const adjusted_errors = new WeakSet();
6474
* @param {Error} error
6575
* @param {Effect} effect
6676
*/
67-
function adjust_error(error, effect) {
77+
function get_adjustments(error, effect) {
6878
if (adjusted_errors.has(error)) return;
6979
adjusted_errors.add(error);
7080

@@ -83,17 +93,28 @@ function adjust_error(error, effect) {
8393
context = context.p;
8494
}
8595

86-
define_property(error, 'message', {
87-
value: error.message + `\n${component_stack}\n`
88-
});
96+
return {
97+
message: error.message + `\n${component_stack}\n`,
98+
stack: error.stack
99+
?.split('\n')
100+
.filter((line) => !line.includes('svelte/src/internal'))
101+
.join('\n')
102+
};
103+
}
104+
105+
/**
106+
* @param {Error} error
107+
*/
108+
function apply_adjustments(error) {
109+
const adjusted = adjustments.get(error);
110+
111+
if (adjusted) {
112+
define_property(error, 'message', {
113+
value: adjusted.message
114+
});
89115

90-
if (error.stack) {
91-
// Filter out internal modules
92116
define_property(error, 'stack', {
93-
value: error.stack
94-
.split('\n')
95-
.filter((line) => !line.includes('svelte/src/internal'))
96-
.join('\n')
117+
value: adjusted.stack
97118
});
98119
}
99120
}

0 commit comments

Comments
 (0)