spoonconsulting / cordova-plugin-background-upload

Cordova plugin for background upload
Apache License 2.0
51 stars 54 forks source link

Errors during init doesn't throw and event for the callback #276

Open jomorr-jkl opened 1 year ago

jomorr-jkl commented 1 year ago

If there is an error during init, there is no event thrown so the error can't be caught in the callback.

uploadId = self.crypto.randomUUID();

var payload = {
    id: uploadId,
    filePath: fileUri,
    fileKey: "file",
    serverUrl: serverUri,
    notificationTitle: "Uploading videos",
    headers: {},
    parameters: {}
};

uploader = FileTransferManager.init({}, function (event) {
    if (event.state == 'UPLOADED') {
        console.log("video uploader - upload complete"+ JSON.stringify(event));
    }
    else if (event.state == 'FAILED') {
        if (event.id) {
            console.log("video uploader -  "+ event.id + " has failed "+ event.error);
        }
        else {
            console.log("video uploader caught an error: " + event.error);
        }
    }
    else if (event.state == 'UPLOADING') {
        console.log("video uploader - " + event.id + " progress: " + event.progress + "%");
    }
});

uploader.startUpload(payload);

There is an error accessing the file defined in the payload object, resulting in an error - but there is no way to catch the error so it can be handled.

2023-10-06 07:57:00.207 11546-11546 chromium                com.appname                        I  [INFO:CONSOLE(152)] "20231006075700: videos.html: uploadEventVideo(): starting video file upload
                                                                                                    ", source: file:///android_asset/www/shared/scripts/shared.js (152)
2023-10-06 07:57:00.232 11546-11546 chromium                com.appname                        I  [INFO:CONSOLE(152)] "20231006075700: videos.html: prepareUpload(): background transfer payload {"id":"71f3e9ea-6977-423a-ae5c-ce3fe2bb4e65","filePath":"file:///sdcard/.transforms/synthetic/picker/0/com.android.providers.media.photopicker/media/1000006860.mp4","fileKey":"file","serverUrl":"https://example.net/send_backgroundFile.php","notificationTitle":"Uploading videos","headers":{},"parameters":{}}
                                                                                                    ", source: file:///android_asset/www/shared/scripts/shared.js (152)
2023-10-06 07:57:00.232 11546-11546 chromium                com.appname                        I  [INFO:CONSOLE(152)] "20231006075700: videos.html: prepareUpload(): background transfer init start
                                                                                                    ", source: file:///android_asset/www/shared/scripts/shared.js (152)
2023-10-06 07:57:00.240 11546-11799 PluginManager           com.appname                        D  getPlugin - put: FileTransferBackground
2023-10-06 07:57:00.273 11546-11838 CordovaBackgroundUpload com.appname                        D  startUpload: Starting worker via work manager
2023-10-06 07:57:00.280 11546-11838 CordovaBackgroundUpload com.appname                        D  eventLabel=Uploader starting uploads via worker0
2023-10-06 07:57:00.323 11546-11957 Compatibil...geReporter com.appname                        D  Compat change id reported: 194532703; UID 10686; state: ENABLED
2023-10-06 07:57:00.339 11546-11957 ConnectivityManager     com.appname                        D  StackLog: [android.net.ConnectivityManager.sendRequestForNetwork(ConnectivityManager.java:4322)] [android.net.ConnectivityManager.registerDefaultNetworkCallbackForUid(ConnectivityManager.java:4886)] [android.net.ConnectivityManager.registerDefaultNetworkCallback(ConnectivityManager.java:4853)] [android.net.ConnectivityManager.registerDefaultNetworkCallback(ConnectivityManager.java:4827)] [androidx.work.impl.utils.NetworkApi24.registerDefaultNetworkCallbackCompat(NetworkApi24.kt:28)] [androidx.work.impl.constraints.trackers.NetworkStateTracker24.startTracking(NetworkStateTracker.kt:139)] [androidx.work.impl.constraints.trackers.ConstraintTracker.addListener(ConstraintTracker.kt:57)] [androidx.work.impl.constraints.controllers.ConstraintController.replace(ConstraintController.kt:82)] [androidx.work.impl.constraints.WorkConstraintsTrackerImpl.replace(WorkConstraintsTracker.kt:92)] [androidx.work.impl.background.greedy.GreedyScheduler.schedule(GreedyScheduler.java:170)] [androidx.work.impl.Schedulers.schedule(Schedulers.java:119)] [androidx.work.impl.utils.EnqueueRunnable.scheduleWorkInBackground(EnqueueRunnable.java:135)] [androidx.work.impl.utils.EnqueueRunnable.run(EnqueueRunnable.java:95)] [androidx.work.impl.utils.SerialExecutorImpl$Task.run(SerialExecutorImpl.java:96)] [java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)] [java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)] [java.lang.Thread.run(Thread.java:1012)]
2023-10-06 07:57:00.345 11546-11546 CordovaBackgroundUpload com.appname                        D  Task succeeded: e6b53e05-777a-4536-ba68-944cd915b314
2023-10-06 07:57:00.346 11546-11546 CordovaBackgroundUpload com.appname                        D  eventLabel='Uploader failed inexplicably' error='Data {output_failure_canceled : false, output_is_error : true, output_failure_reason : File not found !, output_id : e0450a9d-f0a7-45dc-8eac-5bfcfd317450, }'
2023-10-06 07:57:00.372 11546-11546 CordovaBackgroundUpload com.appname                        D  Task succeeded: 53988ed2-1ce3-4085-901b-641865cb8053
2023-10-06 07:57:00.372 11546-11546 CordovaBackgroundUpload com.appname                        D  eventLabel='Uploader failed inexplicably' error='Data {}'
2023-10-06 07:57:00.384 11546-11960 CordovaBackgroundUpload com.appname                        E  doWork: File not found !
                                                                                                    java.io.FileNotFoundException: /sdcard/.transforms/synthetic/picker/0/com.android.providers.media.photopicker/media/1000007028.mp4: open failed: EFAULT (Bad address)
                                                                                                        at libcore.io.IoBridge.open(IoBridge.java:574)
                                                                                                        at java.io.FileInputStream.<init>(FileInputStream.java:160)
                                                                                                        at com.spoon.backgroundfileupload.UploadTask.createRequest(UploadTask.java:345)
                                                                                                        at com.spoon.backgroundfileupload.UploadTask.doWork(UploadTask.java:178)
                                                                                                        at androidx.work.Worker$1.run(Worker.java:82)
                                                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
                                                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
                                                                                                        at java.lang.Thread.run(Thread.java:1012)
                                                                                                    Caused by: android.system.ErrnoException: open failed: EFAULT (Bad address)
                                                                                                        at libcore.io.Linux.open(Native Method)
                                                                                                        at libcore.io.ForwardingOs.open(ForwardingOs.java:563)
                                                                                                        at libcore.io.BlockGuardOs.open(BlockGuardOs.java:274)
                                                                                                        at libcore.io.ForwardingOs.open(ForwardingOs.java:563)
                                                                                                        at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:8624)
                                                                                                        at libcore.io.IoBridge.open(IoBridge.java:560)
                                                                                                        at java.io.FileInputStream.<init>(FileInputStream.java:160) 
                                                                                                        at com.spoon.backgroundfileupload.UploadTask.createRequest(UploadTask.java:345) 
                                                                                                        at com.spoon.backgroundfileupload.UploadTask.doWork(UploadTask.java:178) 
                                                                                                        at androidx.work.Worker$1.run(Worker.java:82) 
                                                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
                                                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644) 
                                                                                                        at java.lang.Thread.run(Thread.java:1012) 
2023-10-06 07:57:00.386 11546-11957 WM-WorkerWrapper        com.appname                        I  Worker result SUCCESS for Work [ id=53988ed2-1ce3-4085-901b-641865cb8053, tags={ com.spoon.backgroundfileupload.UploadTask, work_tag_upload } ]
2023-10-06 07:57:00.395 11546-11546 ConnectivityManager     com.appname                        D  StackLog: [android.net.ConnectivityManager.unregisterNetworkCallback(ConnectivityManager.java:4996)] [androidx.work.impl.utils.NetworkApi21.unregisterNetworkCallbackCompat(NetworkApi21.kt:31)] [androidx.work.impl.constraints.trackers.NetworkStateTracker24.stopTracking(NetworkStateTracker.kt:154)] [androidx.work.impl.constraints.trackers.ConstraintTracker.removeListener(ConstraintTracker.kt:73)] [androidx.work.impl.constraints.controllers.ConstraintController.replace(ConstraintController.kt:80)] [androidx.work.impl.constraints.WorkConstraintsTrackerImpl.replace(WorkConstraintsTracker.kt:92)] [androidx.work.impl.background.greedy.GreedyScheduler.removeConstraintTrackingFor(GreedyScheduler.java:243)] [androidx.work.impl.background.greedy.GreedyScheduler.onExecuted(GreedyScheduler.java:229)] [androidx.work.impl.Processor.onExecuted(Processor.java:385)] [androidx.work.impl.Processor$FutureListener.run(Processor.java:484)]
2023-10-06 07:57:00.407 11546-11546 CordovaBackgroundUpload com.appname                        D  Task succeeded: 53988ed2-1ce3-4085-901b-641865cb8053
2023-10-06 07:57:00.407 11546-11546 CordovaBackgroundUpload com.appname                        D  eventLabel='Uploader failed inexplicably' error='Data {output_failure_canceled : false, output_is_error : true, output_failure_reason : File not found !, output_id : e0450a9d-f0a7-45dc-8eac-5bfcfd317450, }'

Even worse is there is no way to purge the upload queue to remove this problem upload, so no new uploads work without uninstalling and reinstalling the app (opened a separate issue for that)

TOMassey commented 9 months ago

I'm echoing this issue.

I've placed "console.log('hitting startUpload', payload);" 1 line before the startUpload call, and "console.log('hitting inside event')" in the first line of the event function of FileTransferManager.init.

The "hitting startUpload" log hits, and the payload looks 100% correct. The "hitting inside event" never fires. No console errors. No signs of anything being requested in the Network tab when viewing the app in dev tools.

Worth noting:

So it appears that the issue is occurring due to using a...correct filePath format?

TOMassey commented 9 months ago

I'm echoing this issue.

I've placed "console.log('hitting startUpload', payload);" 1 line before the startUpload call, and "console.log('hitting inside event')" in the first line of the event function of FileTransferManager.init.

The "hitting startUpload" log hits, and the payload looks 100% correct. The "hitting inside event" never fires. No console errors. No signs of anything being requested in the Network tab when viewing the app in dev tools.

Worth noting:

  • The plugin code/script is left untouched, so no changes to FileTransferManager.js
  • If I set the value of filePath in the payload to anything other than "file:///source/ ..." format, the startUpload function will break, and the event will fire an error message.
  • Trying to tinker with FileTransferManager.js--more specifically, the inner-workings of the startUpload function--will (obviously) cause an error, which will trigger the event to fire an error message. (And yes, I did do a clean re-install of the plugin after attempting this.)

So it appears that the issue is occurring due to using a...correct filePath format?

One note I forgot to include: my project is a Capacitor project (v.5.5.0). I'm wondering if there's a compatibility issue here.....

TOMassey commented 9 months ago

I ran an npm update command for one of my other (working) cordova plugins, in order to see if an "unsupported engine" warning would pop up for the background-upload plugin, and sure enough....

npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: '@spoonconsulting/cordova-plugin-background-upload@4.0.10',
npm WARN EBADENGINE   required: { cordova: '>=8.0.0', node: '14.x', npm: '6.x' },
npm WARN EBADENGINE   current: { node: 'v18.16.1', npm: '9.5.1' }

I'm not super-duper positive that this is the cause of our issue, but right now it's the only lead I've got. Despite the startUpload function reaching the execcommand (which is a Cordova core function built into Capacitor--makes sense), nothing is happening after that execfunction completes successfully.

zfir commented 9 months ago

Hello @TOMassey,

The warning you have provided in not the problem. This is a Node version update in package.json of the library.

As for the callback error you are encountering, we have not tested on Capacitor yet. We will not work on this soon but it is in our roadmap.

For now, a pull request is most welcome.