From e6e27ac0b9af015ebc67b9dc5605fc7bebe70942 Mon Sep 17 00:00:00 2001 From: Tom Spencer Date: Wed, 15 Aug 2018 20:59:03 +0100 Subject: [PATCH] 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). --- .../main/java/com/RNFetchBlob/RNFetchBlobFS.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java b/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java index 76eb4abe1..5574609db 100644 --- a/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java +++ b/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java @@ -548,6 +548,7 @@ static void cp(String path, String dest, Callback callback) { path = normalizePath(path); InputStream in = null; OutputStream out = null; + String message = ""; try { if(!isPathExists(path)) { @@ -571,7 +572,7 @@ static void cp(String path, String dest, Callback callback) { out.write(buf, 0, len); } } catch (Exception err) { - callback.invoke(err.getLocalizedMessage()); + message += err.getLocalizedMessage(); } finally { try { if (in != null) { @@ -580,11 +581,17 @@ static void cp(String path, String dest, Callback callback) { if (out != null) { out.close(); } - callback.invoke(); } catch (Exception e) { - callback.invoke(e.getLocalizedMessage()); + message += e.getLocalizedMessage(); } } + // Only call the callback once to prevent the app from crashing + // with an 'Illegal callback invocation from native module' exception. + if (message != "") { + callback.invoke(message); + } else { + callback.invoke(); + } } /**