Open rap2hpoutre opened 3 years ago
It seems a good idea. We can add onError
prop in the ReactiveBase
to catch errors.
Love it and was about to create another Issue for it! I also think it would be a great Idea to make this for all Errors that might happen, not just the network requests.
Any news about this issue?
Thanks for bringing attention to this. We would like to support this use-case. Also in case someone wants to contribute sooner, happy to provide guidance on how to do this.
@siddharthlatest OK, thank you. I don't know where to start, but I would be glad to tackle this issue. I checked ReactiveBase in web but I did not found where the query is actually done nor where I can catch errors (I see a componentDidCatch
that does not seem to be triggered). Could you give me guidance on how I should start?
@rap2hpoutre The existing componentDidCatch
would not get invoked on network calls error. The best way to catch the network requests errors is to use the StateProvider component.
Check this example https://codesandbox.io/s/optimistic-euclid-2tbm7?file=/src/index.js
@rap2hpoutre As per your use-case, you want to handle the error before ReactiveSearch
processes it. For e.g if ReactiveList
was showing some results and next request caused some error then it should not clear the results.
ReactiveBase
.
<ReactiveBase
onError={(error) => new Promise((resolve, reject) => {
// resolve would forward the error to RS
if (error.code === 401) {
// avoid processing and redirect to login modal
reject(error)
} else {
// forward the error to RS
resolve(error)
}
})}
/>
transformRequest
.
https://github.com/appbaseio/reactivesearch/blob/next/packages/web/src/components/basic/ReactiveBase.js#L102
When a search fails and returns 401 (let's say because the session is not valid anymore), Reactive Search throws an error. I would love to catch it to not trigger an error, since it just means the connection is lost. The problem in throwing an error is that it adds some mess the console and the bug trackers (eg: Sentry). For instance, I would love a clean way to catch the error then redirect the user to a auth.
onError
on components is too late since the error is already thrown.I'm using v3.14.0 (React project)