Skip to content
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

Fail Gracefully In Selection #5229

Open
Nikhil22 opened this issue Dec 17, 2022 · 1 comment · May be fixed by #5407
Open

Fail Gracefully In Selection #5229

Nikhil22 opened this issue Dec 17, 2022 · 1 comment · May be fixed by #5407

Comments

@Nikhil22
Copy link

Nikhil22 commented Dec 17, 2022

Description
Any time I run into an error like "Cannot resolve DOM point/node" pertaining to Slate logic, my app just crashes. Is it not possible to catch these errors (internally) somehow? To fail gracefully?

As a workaround I'm wrapping vulnerable selection logic in try/catch blocks ... but it doesn't feel enough. When the editor itself crashes, only thing that catches this is my ErrorBoundary. But again, I don't want a crash in the first place. I'd like to know how others handle this for production apps.

Expectation
A quiet failure, possibly returning null values, warning, or something else.

Environment
Slate Version: all
Operating System: all
Browser: all
TypeScript Version: all

@Nikhil22
Copy link
Author

Nikhil22 commented Dec 30, 2022

Came up with a solution for ensuring the cursor stays blinking at the previously focused spot, after the editor gets reset due to a save etc.

useEffect(() => {
  if (!mostRecentSelection) return
  
  let domPointAnchor: DOMPoint | null = null
  let domPointFocus: DOMPoint | null = null

  try {
    domPointFocus = ReactEditor.toDOMPoint(editorState, mostRecentSelection.focus)
    domPointAnchor = ReactEditor.toDOMPoint(editorState, mostRecentSelection.anchor)
  } catch(e: any) {}

  if (domPointFocus && domPointAnchor) {
    Transforms.select(editorState, mostRecentSelection as Location)
  }

}, [mostRecentSelection, richNoteContentsControlled])

I store the most recent selection in state. I try to get the DOM point. Luckily toDOMPoint() throws catchable errors. If the domPoint exists, I perform the selection.

I think the Docs should be clear that methods of Transforms do not throw errors and are thus uncatchable. And that errors in functions like ReactEditor.toDomPoint can be caught instead.

@Nikhil22 Nikhil22 changed the title Fail Gracefully Fail Gracefully In Selection Dec 30, 2022
@zbeyens zbeyens linked a pull request Apr 27, 2023 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant