tobydeh / nativescript-download-progress

Download large files in Nativescript with progress events
Apache License 2.0
11 stars 16 forks source link

Download failure is not properly rejected on iOS #24

Closed nikoTM closed 2 years ago

nikoTM commented 3 years ago

When provided a faulty link that raises response code 500, the promise resolves with an empty file.

https://github.com/tobydeh/nativescript-download-progress/blob/b165d990dda6e8f1808970eb7fb8defac830c109/src/download-progress.ios.ts#L120 is being called regardless of the server response. In case of error being empty it resolves with the empty file, but the assumption of error being empty means request succeeded is not correct.

As a workaround, I modified else block to be this:

else {
            if (task && task.response && task.response.statusCode) {
                const statusCode = task.response.statusCode;
                if (statusCode < 200 || statusCode >= 400) {
                    reject('Server responded with status code ' + statusCode);
                    return;
                }
            }
            resolve(destinationFile);
        }

task.response is NSHTTPURLResponse