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.

how to handle network request failed error?

See original GitHub issue

I have a form and want to send it to a server and get a response so I wrote this:

submitForm = async () => {
  fetch("www.somewhere.com", {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(some_data)
  })
  .then((response) => response.json())
  .then((responseJson) => {
     // do something
  })
  .catch((error) => {
      this.setState({server_error: "request failed try again."});
  });
};

But It seems that my catch doesn’t work correctly because if the request fails I get an error from react native like this:

TypeError: Network request failed at XMLHttpRequest.xhr.onerror (C:\Archive\code\current\native\melcom\node_modules\react-native\Libraries\vendor\core\whatwg-fetch.js:504)

Note that I’m not struggling with http or https, I just want to provide a nice error to the user if the request fails due to the internet connection, wrong API, etc. instead of getting react native error.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:18

github_iconTop GitHub Comments

1reaction
Dina07commented, Apr 14, 2019

error.message this.state.error:error.message

1reaction
satkamarcommented, Apr 14, 2019

Have you found a solution for this? I am having the same error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native fetch() Network Request Failed - Stack Overflow
1) To find out the exact error in the logs, I first enabled 'Debug JS Remotely' using Cmd + M on the app...
Read more >
HTTP Fetch fails with "TypeError: Network request ... - GitHub
Using fetch to get/post on a HTTPS web server which is using a valid and trusted but not public CA. Using Chrome and...
Read more >
How to solve 'TypeError: Network request failed' in React ...
two thing : 1. add android:usesCleartextTraffic="true" in your Android manifest android/app/src/main/AndroidManifest.xml2.
Read more >
How to Fix Instagram Error Network Request Failed Problem ...
Instagram #Network #Request #FailedIn this video i will show you How to Fix Fix Instagram Error Network Request Failed Problem in Android ...
Read more >
Network Request Failed/Network Error in React Native using ...
It's just that simple! Start your app as usual but don't forget to give an IP address and a port, this will help...
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