-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
fix: add a warning when the misuse of reset
in an error:boundary
causes an error to be thrown when flushing the boundary content
#16171
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
Open
raythurnvoid
wants to merge
9
commits into
sveltejs:main
Choose a base branch
from
raythurnvoid:fix/@sveltejs/svelte/16134
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
997ccef
Add a warning when the misuse of `reset` in an `error:boundary` cause…
raythurnvoid 6255301
Prevent uncaught errors to make test fails when they are expected and…
raythurnvoid 0554c51
Add tests
raythurnvoid 28b0310
Add changeset
raythurnvoid 0ebb878
reset should just be a noop after the first call
Rich-Harris a49a088
changeset
Rich-Harris 3ad0519
correctly handle errors inside boundary during reset
Rich-Harris 942cda7
handle errors in the correct boundary
Rich-Harris f686128
update changeset
Rich-Harris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte': patch | ||
--- | ||
|
||
fix: handle error in correct boundary after reset |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte': patch | ||
--- | ||
|
||
fix: make `<svelte:boundary>` reset function a noop after the first call |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/svelte/tests/runtime-runes/samples/error-boundary-reset-onerror/_config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { flushSync } from 'svelte'; | ||
import { test } from '../../test'; | ||
|
||
export default test({ | ||
test({ assert, target }) { | ||
const btn = target.querySelector('button'); | ||
|
||
btn?.click(); | ||
|
||
assert.throws(flushSync, 'svelte_boundary_reset_onerror'); | ||
|
||
// boundary content empty; only button remains | ||
assert.htmlEqual(target.innerHTML, `<button>trigger throw</button>`); | ||
} | ||
}); |
17 changes: 17 additions & 0 deletions
17
packages/svelte/tests/runtime-runes/samples/error-boundary-reset-onerror/main.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<script> | ||
let must_throw = $state(false); | ||
|
||
function throw_error() { | ||
throw new Error("error on template render"); | ||
} | ||
</script> | ||
|
||
<svelte:boundary onerror={(_, reset) => reset()}> | ||
{must_throw ? throw_error() : 'normal content'} | ||
|
||
{#snippet failed()} | ||
<div>err</div> | ||
{/snippet} | ||
</svelte:boundary> | ||
|
||
<button onclick={() => must_throw = true}>trigger throw</button> |
28 changes: 28 additions & 0 deletions
28
packages/svelte/tests/runtime-runes/samples/error-boundary-reset-premature/_config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { flushSync } from 'svelte'; | ||
import { test } from '../../test'; | ||
|
||
export default test({ | ||
html: ` | ||
normal content | ||
<button>toggle</button> | ||
`, | ||
|
||
async test({ assert, target, warnings }) { | ||
const [btn] = target.querySelectorAll('button'); | ||
|
||
flushSync(() => btn.click()); | ||
assert.htmlEqual(target.innerHTML, `<div>err</div><button>toggle</button>`); | ||
assert.deepEqual(warnings, []); | ||
|
||
flushSync(() => btn.click()); | ||
assert.htmlEqual(target.innerHTML, `normal content <button>toggle</button>`); | ||
assert.deepEqual(warnings, []); | ||
|
||
flushSync(() => btn.click()); | ||
assert.htmlEqual(target.innerHTML, `<div>err</div><button>toggle</button>`); | ||
|
||
assert.deepEqual(warnings, [ | ||
'A `<svelte:boundary>` `reset` function only resets the boundary the first time it is called' | ||
]); | ||
} | ||
}); |
26 changes: 26 additions & 0 deletions
26
packages/svelte/tests/runtime-runes/samples/error-boundary-reset-premature/main.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<script> | ||
let must_throw = $state(false); | ||
let reset = $state(null); | ||
|
||
function throw_error() { | ||
throw new Error("error on template render"); | ||
} | ||
</script> | ||
|
||
<svelte:boundary onerror={console.error}> | ||
<svelte:boundary onerror={(_, fn) => (reset = fn)}> | ||
{must_throw ? throw_error() : 'normal content'} | ||
|
||
{#snippet failed()} | ||
<div>err</div> | ||
{/snippet} | ||
</svelte:boundary> | ||
</svelte:boundary> | ||
|
||
<button | ||
onclick={() => { | ||
must_throw = !must_throw; | ||
if (reset) reset(); | ||
}}> | ||
toggle | ||
</button> |
28 changes: 28 additions & 0 deletions
28
packages/svelte/tests/runtime-runes/samples/error-boundary-reset-with-error/_config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { flushSync } from 'svelte'; | ||
import { test } from '../../test'; | ||
|
||
export default test({ | ||
test({ assert, target, warnings }) { | ||
const [toggle] = target.querySelectorAll('button'); | ||
|
||
flushSync(() => toggle.click()); | ||
assert.htmlEqual( | ||
target.innerHTML, | ||
// TODO the synthetic stack shouldn't be part of the message here | ||
`<button>toggle</button><p>yikes! in {expression} in undefined</p><button>reset</button>` | ||
); | ||
|
||
const [, reset] = target.querySelectorAll('button'); | ||
flushSync(() => reset.click()); | ||
assert.htmlEqual( | ||
target.innerHTML, | ||
`<button>toggle</button><p>yikes! in {expression} in undefined</p><button>reset</button>` | ||
); | ||
|
||
flushSync(() => toggle.click()); | ||
|
||
const [, reset2] = target.querySelectorAll('button'); | ||
flushSync(() => reset2.click()); | ||
assert.htmlEqual(target.innerHTML, `<button>toggle</button><p>hello!</p>`); | ||
} | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Rich-Harris People might want to auto-fix the state of the application inside the onerror for instance they might decide to show an error snackbar and reset a form without any confirmation from the user
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It won't work — the update is still in progress and everything gets torn. The
svelte_boundary_reset_onerror
message has an example of fixing it by waiting for atick()
before callingreset()