React-native: when using fetch, axios, etc.`catch` doesn't handle the error, instead your app crashes

Created on 2 Apr 2019  路  8Comments  路  Source: facebook/react-native

馃悰 Bug Report

To make sure that our request will be successful, first, we check the internet connection then send our request.
This approach was fine until I faced this issue: consider a situation in which access to a server for some countries is blocked.
So, although the internet connection was ok, each time I was getting network request failed error and my app was just crashing.
Note that this issue is not limited to this problem, it also happens when your internet speed is low, etc.
So, What is the point of catch!? if it doesn't catch the error!?

To Reproduce

react-native init myApp
sending a request to an endpoint which is blocked for your IP.

Expected Behavior

The expected behavior is that, when the connection can't be made, catch gets the error and I can inform the user to use a VPN or leave the app because they are in an embargoed country!

Code Example

NetInfo.isConnected.fetch().then(async isConnected=> {
  if(isConnected){
    try {
       let result = await fetch(MY_REMOTE_SERVER);
       console.log("result: ", result)
    } catch (error) {
       console.error("error: ", error);
    }
  }
  else ToastAndroid.show('No internet', ToastAndroid.SHORT);
});

Environment

React Native Environment Info:
System:
OS: Windows 10
CPU: (4) x64 Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz
Memory: 4.09 GB / 7.90 GB
Binaries:
Yarn: 1.12.3 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
npm: 6.4.1 - C:\Program Files\nodejs\npm.CMD
IDEs:
Android Studio: Version 3.2.0.0 AI-181.5540.7.32.5056338.

Bug Android iOS Locked 馃寪Networking

Most helpful comment

Is it crashing in debug or release mode? In debug mode a console.error will throw up a red screen, but won't cause a crash in release

All 8 comments

what is your React Native version?

what is your React Native version?

"react-native": "0.59.1".

I'm having the same issue on both Android and iOS, but with a different library ("react-native-firebase": "^5.2.3"). I don't know if the two things are related, here the issue on the lib tracker https://github.com/invertase/react-native-firebase/issues/1993.

"react-native": "0.59.3"

Is it crashing in debug or release mode? In debug mode a console.error will throw up a red screen, but won't cause a crash in release

@JKCooper2 yes you right, but until you change your app to release mode, you won't understand it, on the other hand, it bothers a lot when you're in debug mode because you get the red screen and your app restart. If catch could get the error, it would be great. thanks

Is it crashing in debug or release mode? In debug mode a console.error will throw up a red screen, but won't cause a crash in release

oh, I'm sorry you right. I should replace console.error with console.log and it won't crash anymore

oh, I'm sorry you right. I should replace console.error with console.log and it won't crash anymore

馃憢 closing this issue now as I believe this is resolved, if not please let me know.

I wish "Hey, BTW, console.error intentionally crashes your app in development." were more prominently documented and/or configurable.

Was this page helpful?
0 / 5 - 0 ratings