fetch throws in console even though there is try{}catch(){} - reactjs

Does anyone know why I can't handle server error with try{}catch(){}?
There is a console error although I am trying to handle fetch errors within the fetch context and outside of it?
Please refer to the attached image to get a better understanding: Fetch request
I am using react with redux-saga but I think there is nothing related with redux.
I tried executing that same function in the component itself and it still happens.

Related

Handling errors in reducer and async thunk

I would like to create error page which would show up on error. So far I have been able to use try two approaches
react-error-boundary but right now I have a problem, I have an async thunk which is using axios to download some data. The problem I have is that I would like to catch and exception from axios and show my general error page with some info. The problem I have is that react-error-boundary does not catch any exception inside my thunk (or slice).
I was also thinking to error is some general reducer, the problem is that I cannot set one reducer state from another.
What is the proper way to handle this?
Error boundaries are intended to keep an uncaught exception from crashing the page. You can think of them as a safety net for any unexpected errors that you forgot to handle. They're not really intended to be the default way you would choose to display an error to the user. If something goes wrong with your Axios network request you should catch it with a .catch() block. In your catch block you can execute logic to render an error message. If you have a specific error page you could redirect to it in the catch. You could also dispatch a Redux action to conditionally render an error message.

How to fetch data correctly in a react/redux app?

I've seen this question repeated a lot of times, but I'm trying to develop a react/redux app and every time i tried to fetch data before the page is loaded i got a lot of problems.
I feel the problem is the async calls, looks that sometimes when i use the data result of the data fetch, the store hasn't upload yet, so i try to access to undefined variables and i get error.
I've tried to use componentDidMount() and from inside the method trigger an action that fetch the data and actualize the store state for me . I also tried to use componentDidMount() and then componentWillReceiveProps(nextProps) to use the data when i cannot directly use it in the html.
So the problem is that sometimes, not always, the data i want to fetch is already undefined when i use it. Is there a way to avoid that? And How is the correct way to fetch data?
Have you tried using componentWillMount? Everything you put inside componentWillMount will execute before your application gets rendered, unlike componentDidMount where first you get your application rendered, then it executes everything you put inside.
Also, have you every looked at hooks and useEffect function? Try to research hooks cause it will solve a lot of boilerplate with standard lifecycle functions.

Apollo GraphQL local and global error handling

I'm using Apollo to interact with a GraphQL server in a Web application written in React. I'm trying to implement error handling in the application and relying on apollo-link-error for this.
Now, there are 2 categories of errors that I need to handle:
errors that can be handled locally in the component which does the Apollo query or mutation, i.e. an invalid form field on which I need to show contextual error information
errors that can be handled globally, for example by showing a toast notification displaying error details somewhere in the page
Clearly, once the error is handled locally I need it to not be handled globally, because it doesn't make much sense to show an error message next to a form field and a generic error via a toast message.
The first stumbling block I encountered when trying to implement this is that the global error handling logic triggers before the local error handling logic, which prevents me from being able to intercept the error locally and then find a way to prevent the global logic from kicking in.
I created codesandbox example which sets up an ApolloClient in the simplest possible way, uses the http and error links, and uses the react-apollo Query component to do a query for a resource that doesn't exist, generating an error.
I'm handling the error both in the onError callback of the Query component (so local error handling), and in the apollo-link-error handler (so global error handling), and printing to the console the errors.
It shows that the global error handling logic kicks in before the local error handling. I would need it to be the other way around.
I've published a library called react-apollo-network-status which handles exactly this use case. Let me know if it's useful to you!
The opt-in/-out behaviour for treating some errors locally is implemented by setting a context variable on the operation which can be read in the error link.

Redux Saga, error swallowing

I try to add a global error handler on my react app. That work perfecly but I have only one problem when I use saga and a fetch call. Below a simple example :
The error (in setModules) is swallowing (I think by the fetch promise) but I dont understand where and how to correct this behavior. Is this case, the error is never send to the error or unhandledrejection events and google devtool reports that error from the "getModules" function. I tried many changes (like using done instead of then and remove the catch) but no miracle.
You have a catch statement into another catch... Try putting the (error handling logic) try/catch statement in your getModules saga only et viola.
I highly recommend to manage error into the sagas only, not in your gateways functions, that way to avoid this issues.

React-Router client-side error handling after server-side rendering

I'm using server side rendering for my React app but can't wrap my head around the logic for showing error pages when something goes wrong.
For example:
User requests /article/123
Something goes wrong while fetching the article
I would like to show a 500 error page
The server side part was easy. I tell React-Router to serve my error component. So far so good.
But when the page is served, the client-side javascript is executed and it takes over rendering. React-Router see the url /article/123 and loads the component that shows the article (but fails since the data is not present..)
Is there a way to let the client-side know that we want to show the error component instead?
The only think I could think of is the following: Add the error to the global redux state. Before rendering a component, check if the error is present in the global state and show the error component instead.. But the downside of this is that you have to implement that checking logic in all of your components. There should be some kind more elegant way to fix this..
There's a few different ways to implement client-side error handling; I find using Higher Order Components work best. You would create a wrapper component that checks for errors from the server response. If it finds one, serve the appropriate error page. If the HOC doesn't detect an error, serve the component the user originally requested.
Here's a great explanation on how to implement HOC:
https://medium.com/#franleplant/react-higher-order-components-in-depth-cf9032ee6c3e

Resources