React Native application using rn-fetch-blob crashes if server returns an error

2.2k Views Asked by At

I am trying to learning React Native and in the process I am trying to download a file using rn-fetch-blob using the following code

        var url = APIPREFIX + '/image/download/' + image.id;
        var config = {
            path: ((Platform.OS === 'ios') ? RNFetchBlob.fs.dirs.DocumentDir : RNFetchBlob.fs.dirs.DownloadDir) + '/' + image.fileName,
        };
    
        RNFetchBlob.config(config)
            .fetch('GET', url)
            .then((res) => {
                    var path = res.path();
                    
                    if (Platform.OS === 'ios') {
                        RNFetchBlob.ios.previewDocument(path)
                    } else {
                        RNFetchBlob.android.actionViewIntent(path, image.image ? "image/png" : image.type);
                    }
            }).catch((errorMessage) => {
                    console.log(errorMessage);
                    // no special error handling required.
                    // added this line to check if an error is being returned
                });

This works properly when the server returns a file (which will always be an image file). However if the server returns any error (typically 404 or 401) - the application crashes.

adb logcat shows the following lines

05-05 22:40:09.984   518  2945 I system_server: oneway function results will be dropped but finished with status OK and parcel size 4
05-05 22:40:10.038  8846  9054 E AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
05-05 22:40:10.038  8846  9054 E AndroidRuntime: Process: com.downloadTest, PID: 8846
05-05 22:40:10.038  8846  9054 E AndroidRuntime: java.lang.ClassCastException: okhttp3.internal.http.RealResponseBody cannot be cast to com.RNFetchBlob.Response.RNFetchBlobFileResp
05-05 22:40:10.038  8846  9054 E AndroidRuntime:        at com.RNFetchBlob.RNFetchBlobReq.done(RNFetchBlobReq.java:594)
05-05 22:40:10.038  8846  9054 E AndroidRuntime:        at com.RNFetchBlob.RNFetchBlobReq.access$100(RNFetchBlobReq.java:72)
05-05 22:40:10.038  8846  9054 E AndroidRuntime:        at com.RNFetchBlob.RNFetchBlobReq$3.onResponse(RNFetchBlobReq.java:493)
05-05 22:40:10.038  8846  9054 E AndroidRuntime:        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:203)
05-05 22:40:10.038  8846  9054 E AndroidRuntime:        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
05-05 22:40:10.038  8846  9054 E AndroidRuntime:        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
05-05 22:40:10.038  8846  9054 E AndroidRuntime:        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
05-05 22:40:10.038  8846  9054 E AndroidRuntime:        at java.lang.Thread.run(Thread.java:923)
05-05 22:40:10.041   518  2945 W ActivityTaskManager:   Force finishing activity com.downloadTest/.MainActivity
"react": "16.13.1"
"react-native": "^0.64.0"
"rn-fetch-blob": "^0.12.0"
Android Version: 10
Android Simulator Version: 11

Behavior is same across Android Simulator/Android Device/iOS Simulator/iPhone Device.

Some articals here talk about a release 0.12.1 but I am unable to find out how to download it - and if it is relevant. In Some answers it talks about changing AndroidManifest to allow access to file system - but this does not appear to be the problem

I am requesting help to understand how to resolve this or handle this in my code.

0

There are 0 best solutions below