frontarm / navi

🧭 Declarative, asynchronous routing for React.
https://frontarm.com/navi/
MIT License
2.07k stars 71 forks source link

How to create a custom boundary? How to catch custom errors? #193

Open OnkelTem opened 4 years ago

OnkelTem commented 4 years ago

Currently there is only one boundary - NotFoundBoundary which catches only NotFoundError errors. In my app I also need to prevent access to specific pages and for that I would like to create a custom error - e.g.AccessDeniedError and corresponding boundary as well. But I'm not sure how to do it. The original NotFoundBoundary uses NaviContext which is not exported.

import { NaviContext } from './NaviContext'

export const NotFoundBoundary: React.SFC<NotFoundBoundaryProps> = function ErrorBoundary(props: NotFoundBoundaryProps) {
  return (
    <NaviContext.Consumer>
      {context => <InnerNotFoundBoundary context={context} {...props} />}
    </NaviContext.Consumer>
  )
}

So how can one add new error types and effectively catch them?