React Native App crashes on Network error












0















I am developing one document upload app, using react native.
Documents get uploaded if the network is good, but suddenly the app crashes if the network goes down.
For instance - If I am uploading the documents on 4G network then i receive success from API but if the network speed suddenly goes down to 2G network i.e. 20KB/S then I get "NETWORK REQUEST FAILED / NETWORK ERROR" and the app crashes.



I have handled the error using catch block, but still the app crashes, what should I do to avoid this crashing even if the network is not available or if the speed goes down ?



Below is my code -



try{
var form = FormData();
form.append("user_id",this.state.userId);
form.append('file', {
uri: this.state.path,
type: 'image/jpg',
name: 'image.jpg',
});
axios({
method:"POST",
url:"http://xxxxx/xxxx",
timeout:50000,
data:form
})
.then(response => console.log(response))
.catch(error=>console.log(error));

}
catch(error){
console.log(error);
ToastAndroid.show("Internal error, please try again");
}


Note - I have used react-native-image-picker, as the document can be captured using camera OR it could be uploaded from the existing file present in the device.



Also have added all permissions in Manifest file for internet access.



Please help










share|improve this question





























    0















    I am developing one document upload app, using react native.
    Documents get uploaded if the network is good, but suddenly the app crashes if the network goes down.
    For instance - If I am uploading the documents on 4G network then i receive success from API but if the network speed suddenly goes down to 2G network i.e. 20KB/S then I get "NETWORK REQUEST FAILED / NETWORK ERROR" and the app crashes.



    I have handled the error using catch block, but still the app crashes, what should I do to avoid this crashing even if the network is not available or if the speed goes down ?



    Below is my code -



    try{
    var form = FormData();
    form.append("user_id",this.state.userId);
    form.append('file', {
    uri: this.state.path,
    type: 'image/jpg',
    name: 'image.jpg',
    });
    axios({
    method:"POST",
    url:"http://xxxxx/xxxx",
    timeout:50000,
    data:form
    })
    .then(response => console.log(response))
    .catch(error=>console.log(error));

    }
    catch(error){
    console.log(error);
    ToastAndroid.show("Internal error, please try again");
    }


    Note - I have used react-native-image-picker, as the document can be captured using camera OR it could be uploaded from the existing file present in the device.



    Also have added all permissions in Manifest file for internet access.



    Please help










    share|improve this question



























      0












      0








      0


      1






      I am developing one document upload app, using react native.
      Documents get uploaded if the network is good, but suddenly the app crashes if the network goes down.
      For instance - If I am uploading the documents on 4G network then i receive success from API but if the network speed suddenly goes down to 2G network i.e. 20KB/S then I get "NETWORK REQUEST FAILED / NETWORK ERROR" and the app crashes.



      I have handled the error using catch block, but still the app crashes, what should I do to avoid this crashing even if the network is not available or if the speed goes down ?



      Below is my code -



      try{
      var form = FormData();
      form.append("user_id",this.state.userId);
      form.append('file', {
      uri: this.state.path,
      type: 'image/jpg',
      name: 'image.jpg',
      });
      axios({
      method:"POST",
      url:"http://xxxxx/xxxx",
      timeout:50000,
      data:form
      })
      .then(response => console.log(response))
      .catch(error=>console.log(error));

      }
      catch(error){
      console.log(error);
      ToastAndroid.show("Internal error, please try again");
      }


      Note - I have used react-native-image-picker, as the document can be captured using camera OR it could be uploaded from the existing file present in the device.



      Also have added all permissions in Manifest file for internet access.



      Please help










      share|improve this question
















      I am developing one document upload app, using react native.
      Documents get uploaded if the network is good, but suddenly the app crashes if the network goes down.
      For instance - If I am uploading the documents on 4G network then i receive success from API but if the network speed suddenly goes down to 2G network i.e. 20KB/S then I get "NETWORK REQUEST FAILED / NETWORK ERROR" and the app crashes.



      I have handled the error using catch block, but still the app crashes, what should I do to avoid this crashing even if the network is not available or if the speed goes down ?



      Below is my code -



      try{
      var form = FormData();
      form.append("user_id",this.state.userId);
      form.append('file', {
      uri: this.state.path,
      type: 'image/jpg',
      name: 'image.jpg',
      });
      axios({
      method:"POST",
      url:"http://xxxxx/xxxx",
      timeout:50000,
      data:form
      })
      .then(response => console.log(response))
      .catch(error=>console.log(error));

      }
      catch(error){
      console.log(error);
      ToastAndroid.show("Internal error, please try again");
      }


      Note - I have used react-native-image-picker, as the document can be captured using camera OR it could be uploaded from the existing file present in the device.



      Also have added all permissions in Manifest file for internet access.



      Please help







      android react-native






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 24 '18 at 8:51







      Krunal Panchal

















      asked Nov 24 '18 at 8:39









      Krunal PanchalKrunal Panchal

      64




      64
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Thats an Exception. I guess you need to catch the Exception
          BadNetwork or Timeout You need to Catch it and App should not Crash just make sure
          In the Catch You need to Show the Dialog box which says Can't connect to server.
          OR You need to check if you have a Network connection then only Let the user send the Request show Non-cancelable Dialogbox
          Hope that helps .






          share|improve this answer
























          • I've already added try catch block and axios also has an catch block, but I am not able to get even the console.log(), and how can i prevent crashing ? i.e. app should not crash and stay as it is with an error message in toast ? Please help

            – Krunal Panchal
            Nov 24 '18 at 9:28











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456551%2freact-native-app-crashes-on-network-error%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Thats an Exception. I guess you need to catch the Exception
          BadNetwork or Timeout You need to Catch it and App should not Crash just make sure
          In the Catch You need to Show the Dialog box which says Can't connect to server.
          OR You need to check if you have a Network connection then only Let the user send the Request show Non-cancelable Dialogbox
          Hope that helps .






          share|improve this answer
























          • I've already added try catch block and axios also has an catch block, but I am not able to get even the console.log(), and how can i prevent crashing ? i.e. app should not crash and stay as it is with an error message in toast ? Please help

            – Krunal Panchal
            Nov 24 '18 at 9:28
















          0














          Thats an Exception. I guess you need to catch the Exception
          BadNetwork or Timeout You need to Catch it and App should not Crash just make sure
          In the Catch You need to Show the Dialog box which says Can't connect to server.
          OR You need to check if you have a Network connection then only Let the user send the Request show Non-cancelable Dialogbox
          Hope that helps .






          share|improve this answer
























          • I've already added try catch block and axios also has an catch block, but I am not able to get even the console.log(), and how can i prevent crashing ? i.e. app should not crash and stay as it is with an error message in toast ? Please help

            – Krunal Panchal
            Nov 24 '18 at 9:28














          0












          0








          0







          Thats an Exception. I guess you need to catch the Exception
          BadNetwork or Timeout You need to Catch it and App should not Crash just make sure
          In the Catch You need to Show the Dialog box which says Can't connect to server.
          OR You need to check if you have a Network connection then only Let the user send the Request show Non-cancelable Dialogbox
          Hope that helps .






          share|improve this answer













          Thats an Exception. I guess you need to catch the Exception
          BadNetwork or Timeout You need to Catch it and App should not Crash just make sure
          In the Catch You need to Show the Dialog box which says Can't connect to server.
          OR You need to check if you have a Network connection then only Let the user send the Request show Non-cancelable Dialogbox
          Hope that helps .







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 8:49









          i_fahimkhani_fahimkhan

          12




          12













          • I've already added try catch block and axios also has an catch block, but I am not able to get even the console.log(), and how can i prevent crashing ? i.e. app should not crash and stay as it is with an error message in toast ? Please help

            – Krunal Panchal
            Nov 24 '18 at 9:28



















          • I've already added try catch block and axios also has an catch block, but I am not able to get even the console.log(), and how can i prevent crashing ? i.e. app should not crash and stay as it is with an error message in toast ? Please help

            – Krunal Panchal
            Nov 24 '18 at 9:28

















          I've already added try catch block and axios also has an catch block, but I am not able to get even the console.log(), and how can i prevent crashing ? i.e. app should not crash and stay as it is with an error message in toast ? Please help

          – Krunal Panchal
          Nov 24 '18 at 9:28





          I've already added try catch block and axios also has an catch block, but I am not able to get even the console.log(), and how can i prevent crashing ? i.e. app should not crash and stay as it is with an error message in toast ? Please help

          – Krunal Panchal
          Nov 24 '18 at 9:28




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456551%2freact-native-app-crashes-on-network-error%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Ottavio Pratesi

          Tricia Helfer

          15 giugno