In this section we'll see how to handle the failure case from the previous example. Let's suppose that our API function
Api.fetch returns a Promise which gets rejected when the remote fetch fails for some reason.
We want to handle those errors inside our Saga by dispatching a
PRODUCTS_REQUEST_FAILED action to the Store.
We can catch errors inside the Saga using the familiar
In order to test the failure case, we'll use the
throw method of the Generator
In this case, we're passing the
throw method a fake error. This will cause the Generator to break the current flow and execute the catch block.
Of course, you're not forced to handle your API errors inside
catch blocks. You can also make your API service return a normal value with some error flag on it. For example, you can catch Promise rejections and map them to an object with an error field.
Errors in forked tasks bubble up to their parents until it is caught or reaches the root saga. If an error propagates to the root saga the whole saga tree is already terminated. The preferred approach, in this case, to use onError hook to report an exception, inform a user about the problem and gracefully terminate your app.
Why can't I use
onError hook as a global error handler?
Usually, there is no one-size-fits-all solution, as exceptions are context dependent. Consider
onError hook as the last resort that helps you to handle unexpected errors.
What if I don't want an error to bubble? Consider to use safe wrapper. You can find examples here