-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
feat: provide better error messages in DEV #11526
Conversation
🦋 Changeset detectedLatest commit: 5971159 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
packages/svelte/tests/parser-legacy/samples/action-duplicate/output.json
Outdated
Show resolved
Hide resolved
packages/svelte/tests/snapshot/samples/hmr/_expected/client/index.svelte.js
Outdated
Show resolved
Hide resolved
@Rich-Harris I addressed the issues you mentioned. This PR wasn't finished yesterday when you reviewed it and it had some bugs and missing pieces still yet to do. The stack traces now properly work with sourcemaps. |
Co-authored-by: Rich Harris <[email protected]>
Co-authored-by: Rich Harris <[email protected]>
if (effect_name) { | ||
// If we have an effect name, it means it happened in the template, $effect or $effect.pre. | ||
Object.defineProperty(error, '__skip_hydration_retry', { | ||
value: true, | ||
enumerable: false | ||
}); | ||
} |
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.
this heuristic feels brittle. if an error occurs during component initialisation, effect_name
is empty, and skip_retry
will be false. AFAICT there's only one way a hydration mismatch error will be thrown, and that's e.hydration_missing_marker_close()
in hydration.js
. What if we just check whether the caught error had that code, rather than this __skip_hydration_retry
stuff?
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.
Agreed. although I can still see effect_name
work on init as we set the function name before calling the function now. Although I wonder if the DOM traversal logic might encounter it too?
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.
Are you sure this is the only way this can happen? What if the DOM was modified by some extension and a bogus element was inserted, so the walking mechanism fails and a delete everything + remount would fix it?
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.
delete everything + remount would fix it?
That's not really a fix though, remounting everything is a last resort and they should really fix their problem, no?
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.
What would they be able to fix if a browser extension messes with the dom?
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.
Bad browser extensions break every framework, regardless of us trying to fix it during hydration. The extension will just use a MutationObserver to try and re-apply their changes after – such as Google Translate or ad blockers. This has plagued every library/framework since the dawn of time too. I remember having issues with this and jQuery + ad blocker back in the day!
This was particularly complex with Lexical because of contentEditable. The only thing you can do is let people complain and then point them to turning off their extension. You can go further and add a MutationObserved in DEV that warns on mutations outside of Svelte, but that seems like a step too far for us to go.
We should never optimise for bad web extensions, it's a losing battle. What we can maybe do is add line numbers to the DOM traversal logic and supply the expected element name:
var node = $.sibling(div, [23, 0, 'span']);
var text = $.child(div, [24, 0, 'text']);
Or maybe we can reference the line numbers from the template somehow. @Rich-Harris any ideas?
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.
this seems like overkill to me (and what if the element was wrong but had the same name? we'd still be screwed)
Today, Svelte 5's error message is a bit lacking. This PR aims to make the error messages much better in DEV. Previously, you'd get given the error message and some stack that likely meant nothing. With this PR, you now get the component
stack too along with a clearer message. At also lets you know if the error occurred in different types of effect.