slate: Slate throws exceptions too liberally in relation to selection failures
Hello,
I would like to start a discussion about error handling in Slate which I think is too strict as Errors are often thrown for things that could just as easily be ignored or replaced by a warning. These are in functions like toDOMPoint or toSlateRange when Slate is trying to do things like map a HTMLElement to a Node but cannot because there is no direct mapping (the HTMLElement is created indirectly by a Slate Node). In many cases Slate will throw an Error that will take down the whole page. I understand that various attributes can be applied to have Slate ignore certain regions of the editor but I’ve still found it a bit of a fight and I really don’t understand why Slate can’t just return early in cases where no DOM-to-Slate mapping exists. In fact, I have forked Slate and disabled these Errors (here and here) and made Slate return early and while this fork is a WIP, I can tell you that my app is much better for it - it’s less work for me and I will still find problems where things aren’t wired-up correctly and I no longer have anxiety of deploying to production and worrying about the WSOD when the user’s only crime was clicking on an empty <div/> (I know prod apps should have error handling but again it feels like I am having to handle more errors than is really necessary).
Some ideas:
- As above, log a warning like React does when in development mode and only use
Errorwhen it is absolutely necessary (cannot be recovered/ignored). - Maybe Slate could add a
onErrorcallback to its API for users to learn about errors
Thoughts?
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 62
- Comments: 16 (3 by maintainers)
So far the only error that have been really problematic for me is this one (Cannot resolve a DOM point from Slate point):
Tracked in own issue here: https://github.com/ianstormtaylor/slate/issues/3575 (my comment here https://github.com/ianstormtaylor/slate/issues/3575#issuecomment-620568898)
It makes collaborative editing near impossible, because as far as I can tell, there is no way we are able to adjust the selection when a new value is coming in through props before
slate-reacttries to naively restore the current user selection to a potential invalid DOM-range.Hi @CameronAckermanSEL, I use
componentDidCatchin production as it is necessary whether using Slate or not. However, it feels like Slate is the source of too many errors and that’s the key point. There is also the development experience when first getting started to consider, too - here people might not have proper error handling in place and have to constantly restart/refresh/whatever due to errors for things they probably don’t even care about.Slate seems to think it has to do something based on every interaction with the editor otherwise there is a bug but I don’t think this is right. I guess raising errors was just the easiest approach for the library devs when trying to get 0.50 ready but I don’t think this is the best longterm approach for the library consumers who should ideally decide what is critical and what is not.
Thanks for the response @dylans. I’ll we looking forward for it: in its current implementation we can not go to production with slate due to this issue.
I was developing an editor with Slate and React, but found several situations where I can not avoid the errors thrown by it: for example, when using Katex library to render math formulas.
Of course we can add an error boundary to the
slate-reactEditablecomponent; but when aCannot resolve a DOM point from Slate pointerror is thrown theEditablecomponent will crash anyway, and the whole flow is lost.Just wonder if there was some work done in the direction mentioned by @mpkelly and @cameracker
@karlludwigweise my fork wasn’t intended to fix #3575 per se but only stop Slate from raising errors for things my app does not care about, like clicking on some structural HTML elements. I still think Slate should be changed to return
Node | nullrather than just returningNodeand raising an Error if it can’t do so. It should be the library consumers job to decide what is critical and what is not IMHO.Should also point out that my fork is a quick and dirty fix done with a simple code search. It was not intended to be a community fix and I only mentioned it above to make it clear which parts of the Slate codebase I think should be changed.
Addressed in https://github.com/ianstormtaylor/slate/pull/5407
Discussed with @12joan on that one. We’ve ended up on a design that uses a separate “staging” editor that is not rendered:
The goal is to have a system that prevents:
The alternative would have been to not use a staging editor and “rollback” faulty operations but I’m not comfortable with implementing that design.
As for the error throwing debate, there are many opinions:
nullon “error”For functions having
editoras parameter, the common solution would be an editor methodonErrorso we can pick one of the above behavior. For functions not having access toeditor, we can’t please everyone. We’ll either need to pass a new optiononErroron all of these methods, choose one of the 3 above behaviors or reuse theScrubberdesign https://github.com/ianstormtaylor/slate/pull/4999.PR is WIP.
A tale from a developers first experience with slate:
We researched a buch of richtext editors and decided that slate would make the best fit for our prupose (react-admin and a custom markdown editor). We followed the installation guide and the first thing that hit us was the very error message @skogsmaskin erorted as #3575.
I’ve seen a fix in a fork from @mpkelly but that’s no long term fix.
My 2 cents are: When slate throws an error, it should not take the application down with it. And for the specific error #3575 a smart default would fix most cases.
Current categories of selection failures (from this thread):
The DOM selection is on a node that is not represented in the slate value
An external event removes a node that is selected by slate
An external event changes a node that is selected by slate
setValue. This probably happened because the node that was selected had an address change because immer, but unsure why the path based selection throws.Suggestions:
Provide support for a user defined callback for responding to selection failures, this will allow a user to move the selection.
Have a default selection that slate moves to if it fails
Have slate emit a warning and stop whatever it was doing when selection failed, or silently fail
@antondc I’ve been using katex for math rendering and haven’t run into the issues you mentioned. If you can post a reproducible example on codepen or something that would be useful.
I do think the error could be more detailed sometimes and provide a path of action for resolving it.
I don’t believe much has progressed in this area, though the topic does come up pretty regularly, and along with things like #4769 . It’s an area I too would like to see improve this year.
@skogsmaskin I’ve been working around it by storing the selection, then doing
then changing the value, and afterwards restoring the selection
I am not suggesting this horrible hack as a viable workaround. I have been using this because I am super annoyed at this bug and wanted to move on to something else while still avoiding the crashes.