From 4782e0302c8feb4a3f631e5d064aad3564fc55ee Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Mon, 20 Feb 2023 14:18:24 +0000 Subject: [PATCH] feat: Improve exception thrown on failure to upload/download This should make #8552 much easier to diagnose. --- .../StorageClientImpl.DownloadObject.cs | 10 ++-------- .../StorageClientImpl.UploadObject.cs | 14 ++------------ 2 files changed, 4 insertions(+), 20 deletions(-) diff --git a/apis/Google.Cloud.Storage.V1/Google.Cloud.Storage.V1/StorageClientImpl.DownloadObject.cs b/apis/Google.Cloud.Storage.V1/Google.Cloud.Storage.V1/StorageClientImpl.DownloadObject.cs index 31a1b68816c3..1ea18d5085a1 100644 --- a/apis/Google.Cloud.Storage.V1/Google.Cloud.Storage.V1/StorageClientImpl.DownloadObject.cs +++ b/apis/Google.Cloud.Storage.V1/Google.Cloud.Storage.V1/StorageClientImpl.DownloadObject.cs @@ -131,10 +131,7 @@ private Object DownloadObjectImpl( progress.Report(InitialDownloadProgress.Instance); } var result = downloader.Download(uri, destination); - if (result.Status == DownloadStatus.Failed) - { - throw result.Exception; - } + result.ThrowOnFailure(); // This will have been populated by the downloader. return metadata; } @@ -160,10 +157,7 @@ private Task DownloadObjectAsyncImpl( progress.Report(InitialDownloadProgress.Instance); } var result = await downloader.DownloadAsync(uri, destination, cancellationToken).ConfigureAwait(false); - if (result.Status == DownloadStatus.Failed) - { - throw result.Exception; - } + result.ThrowOnFailure(); // This will have been populated by the downloader. return metadata; }); diff --git a/apis/Google.Cloud.Storage.V1/Google.Cloud.Storage.V1/StorageClientImpl.UploadObject.cs b/apis/Google.Cloud.Storage.V1/Google.Cloud.Storage.V1/StorageClientImpl.UploadObject.cs index 6cff23bdb503..ddc8cb103bfc 100644 --- a/apis/Google.Cloud.Storage.V1/Google.Cloud.Storage.V1/StorageClientImpl.UploadObject.cs +++ b/apis/Google.Cloud.Storage.V1/Google.Cloud.Storage.V1/StorageClientImpl.UploadObject.cs @@ -15,7 +15,6 @@ using Google.Api.Gax; using Google.Apis.Storage.v1; using Google.Apis.Upload; -using Google.Cloud.Storage.V1; using System; using System.IO; using System.Threading; @@ -149,7 +148,7 @@ internal UploadHelper( internal Object Execute() { _mediaUpload.Upload(); - CheckFinalProgress(); + _mediaUpload.GetProgress().ThrowOnFailure(); var result = _mediaUpload.ResponseBody; var hash = _crc == null ? result.Crc32c : Convert.ToBase64String(_crc.GetHash()); if (hash != result.Crc32c) @@ -171,7 +170,7 @@ internal Object Execute() internal async Task ExecuteAsync(CancellationToken cancellationToken) { await _mediaUpload.UploadAsync(cancellationToken).ConfigureAwait(false); - CheckFinalProgress(); + _mediaUpload.GetProgress().ThrowOnFailure(); var result = _mediaUpload.ResponseBody; var hash = _crc == null ? result.Crc32c : Convert.ToBase64String(_crc.GetHash()); if (hash != result.Crc32c) @@ -192,15 +191,6 @@ internal async Task ExecuteAsync(CancellationToken cancellationToken) } return result; } - - private void CheckFinalProgress() - { - var finalProgress = _mediaUpload.GetProgress(); - if (finalProgress.Exception != null) - { - throw finalProgress.Exception; - } - } } } }