Browse Source

Fix RuntimeException

Fixes a case where `callback` was called twice when something went wrong in the `cp` operation, leading to a `java.lang.RuntimeException: Illegal callback invocation from native module. This callback type only permits a single invocation from native code.`

This is the same fix [as seen in the original repo](https://github.com/wkh237/react-native-fetch-blob/pull/408).
Tom Spencer 6 years ago
parent
commit
e6e27ac0b9
No account linked to committer's email address
1 changed files with 10 additions and 3 deletions
  1. 10
    3
      android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java

+ 10
- 3
android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java View File

@@ -548,6 +548,7 @@ class RNFetchBlobFS {
548 548
         path = normalizePath(path);
549 549
         InputStream in = null;
550 550
         OutputStream out = null;
551
+        String message = "";
551 552
 
552 553
         try {
553 554
             if(!isPathExists(path)) {
@@ -571,7 +572,7 @@ class RNFetchBlobFS {
571 572
                 out.write(buf, 0, len);
572 573
             }
573 574
         } catch (Exception err) {
574
-            callback.invoke(err.getLocalizedMessage());
575
+            message += err.getLocalizedMessage();
575 576
         } finally {
576 577
             try {
577 578
                 if (in != null) {
@@ -580,11 +581,17 @@ class RNFetchBlobFS {
580 581
                 if (out != null) {
581 582
                     out.close();
582 583
                 }
583
-                callback.invoke();
584 584
             } catch (Exception e) {
585
-                callback.invoke(e.getLocalizedMessage());
585
+                message += e.getLocalizedMessage();
586 586
             }
587 587
         }
588
+        // Only call the callback once to prevent the app from crashing
589
+        // with an 'Illegal callback invocation from native module' exception.
590
+        if (message != "") {
591
+            callback.invoke(message);
592
+        } else {
593
+            callback.invoke();
594
+        }
588 595
     }
589 596
 
590 597
     /**