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

Add InvalidAutomergeUrlError #281

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

neftaly
Copy link
Collaborator

@neftaly neftaly commented Jan 23, 2024

This PR exports a custom error for invalid automerge URLs, as per #268. The intention is to allow error instanceof InvalidAutomergeUrlError to be used in try/catch blocks and react error boundaries.

For example:

import { InvalidAutomergeUrlError } from 'automerge-repo'
import { ErrorBoundary } from 'react-error-boundary'

export const Bootstrap = () => {
  const handle = useBootstrap({
    onNoDocument: (repo) => {
      const handle = repo.create()
      handle.change((d) => Object.assign(d, { foo: {} }))
      return handle
    }
  })

  const [state] = useDocument(handle?.url)
  if (!state) return (<LoadingDocument />)

  return <Client automergeUrl={handle?.url} />
}

export const ErrorFallback = ({ error }) => {
  if (error instanceof InvalidAutomergeUrlError) {
    return (<p>Invalid document URL</p>)
  }
  return (<p>{error.message}</p>)
}

export const App = () => {
  return (
    <ErrorBoundary FallbackComponent={ErrorFallback}>
      <Bootstrap />
    </ErrorBoundary>
  )
}

@neftaly
Copy link
Collaborator Author

neftaly commented Jan 23, 2024

There are a bunch of caveats around extending builtin Error constructors, as they can't always be transpiled. I am using setPrototypeOf as a workaround, the instantiation overhead shouldn't be an issue. I'm assuming IE10 isn't supported.

@pvh
Copy link
Member

pvh commented Jan 24, 2024

Yeah, I think this is basically fine -- there's another similar pattern in useTimeout (if you look for Error). Probably we should create a helper for this pattern and collect errors somewhere and set a linting rule... but that's a lot more work.

@neftaly
Copy link
Collaborator Author

neftaly commented Jan 24, 2024

I would also like a way to link the errors to a documentation page

@pvh pvh force-pushed the main branch 2 times, most recently from e61f8e3 to d3d1a7d Compare July 26, 2024 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants