How to catch and handle GetBytesAsync error when offline using Firebase Storage?

Viewed 211

I have a Firebase app where I download images. I'm working on how the app would react when offline. With my DownloadImage Method I get an error that I can't seem to properly catch and handle:

ApplicationException: GetFutureStatus(handle) == kFutureStatusPending Rethrow as ApplicationException: backing->status != kFutureStatusComplete

I know the DownloadImage shouldn't work, because I'm offline. However, I can't seem to catch and handle this error, so that the rest of my code can continue.

This is my code for the DownloadImage Method:

public static async Task<ImageCallback> DownloadImage(ArchProject project, ImageSector sector, string fileName)
        {
            ImageCallback _callback = new ImageCallback();
            const long maxAllowedSize = 1 * 2048 * 2048;

            StorageReference img_ref = storage.RootReference.Child(ProjectsKey).Child(project.data.ID)
                .Child(ImagesKey).Child(sector.ToString()).Child(fileName);

            if (Application.internetReachability == NetworkReachability.NotReachable)
            {
                _callback.isSuccess = false;
                _callback.errorMessage = "Please connect to the internet to download images";
                return _callback;
            }

            return await img_ref.GetBytesAsync(maxAllowedSize).ContinueWith((Task<byte[]> task) =>
            {
                if (task.IsFaulted || task.IsCanceled)
                {
                    _callback.isSuccess = false;

                    if (task.Exception != null)
                        _callback.errorMessage = task.Exception.ToString();
                    else
                        _callback.errorMessage = "Unable to connect";

                    _callback.fileName = fileName;
                    return _callback;
                }
                else
                {
                    byte[] fileContents = task.Result;
                    _callback.isSuccess = true;
                    _callback.fileName = fileName;
                    _callback.imageBytes = fileContents;
                    return _callback;
                }
            });
        }

(I bypass the internetReachability check to see how it would react if the device thinks it has internet, but actually doesn't. That is when I get the error)

Can anyone explain where the error comes from and how I can catch/handle it properly?

0 Answers
Related