question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

🐛 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® Core™ 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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
JKCooper2commented, Apr 7, 2019

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

2reactions
Alfredo-Delgadocommented, Sep 26, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

when using fetch, axios, etc.`catch` doesn't handle the error ...
I couldn't find the problem because expected the catch to print the error, but my app was just crashing. Now that I know...
Read more >
Handling Failed HTTP Responses With fetch()
Per MDN, the fetch() API only rejects a promise when a “network error is encountered, although this usually means permissions issues or similar....
Read more >
Let It Crash: Best Practices for Handling Node.js Errors on ...
When a JavaScript error is not properly handled, an uncaughtException is emitted. These suggest the programmer has made an error, and they ...
Read more >
setInterval refreshes the app instead of updating the state- ...
React native hot reloading refreshes but not updating the changes · when using fetch, axios, etc.`catch` doesn't handle the error, instead your app...
Read more >
mzabriskie/axios
Let me provide a little explanation for the clarification: Our react app is using fetch() method of window object, for the http calls...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found