From 3522c4967fb5b7b7f37b90525a258ea980337291 Mon Sep 17 00:00:00 2001 From: JinmingHu Date: Wed, 27 Jan 2021 15:31:36 +0800 Subject: [PATCH] throw exception Azure::Core::RequestFailedException or those inherit from that (#1463) * throw exception Azure::Core::RequestFailedException or those inherits from that * changelog --- sdk/storage/azure-storage-blobs/CHANGELOG.md | 1 + .../inc/azure/storage/blobs/blob_options.hpp | 9 ++++++++- sdk/storage/azure-storage-blobs/src/blob_client.cpp | 8 ++++---- .../azure-storage-blobs/src/blob_container_client.cpp | 6 ------ .../azure-storage-blobs/src/blob_sas_builder.cpp | 2 +- .../inc/azure/storage/common/storage_exception.hpp | 5 +++-- .../src/datalake_file_system_client.cpp | 5 ++++- .../src/datalake_path_client.cpp | 3 ++- .../src/datalake_sas_builder.cpp | 2 +- .../src/share_file_client.cpp | 11 ++++++----- .../src/share_sas_builder.cpp | 2 +- 11 files changed, 31 insertions(+), 23 deletions(-) diff --git a/sdk/storage/azure-storage-blobs/CHANGELOG.md b/sdk/storage/azure-storage-blobs/CHANGELOG.md index 7fc8dda69..9e55c1bbd 100644 --- a/sdk/storage/azure-storage-blobs/CHANGELOG.md +++ b/sdk/storage/azure-storage-blobs/CHANGELOG.md @@ -15,6 +15,7 @@ - Type for lease duration in requests was changed to `std::chrono::seconds`, in response was changed to enum. - `PublicAccessType::Private` was renamed to `PublicAccessType::None`. - `startsOn` parameter for `GetUserDelegationKey` was changed to optional. +- Removed `IfUnmodifiedSince` from `SetBlobContainerMetadataOptions`. - Return types of `BlobClient::StartCopyFromUri` and `PageBlobClient::StartCopyIncremental` were changed to `StartCopyBlobResult`, supporting poll operations. ## 12.0.0-beta.6 (2020-01-14) diff --git a/sdk/storage/azure-storage-blobs/inc/azure/storage/blobs/blob_options.hpp b/sdk/storage/azure-storage-blobs/inc/azure/storage/blobs/blob_options.hpp index 77c240218..ebaa9b524 100644 --- a/sdk/storage/azure-storage-blobs/inc/azure/storage/blobs/blob_options.hpp +++ b/sdk/storage/azure-storage-blobs/inc/azure/storage/blobs/blob_options.hpp @@ -374,7 +374,14 @@ namespace Azure { namespace Storage { namespace Blobs { /** * @brief Optional conditions that must be met to perform this operation. */ - BlobContainerAccessConditions AccessConditions; + struct : public LeaseAccessConditions + { + /** + * @brief Specify this header to perform the operation only if the resource has been + * modified since the specified time. This timestamp will be truncated to second. + */ + Azure::Core::Nullable IfModifiedSince; + } AccessConditions; }; /** diff --git a/sdk/storage/azure-storage-blobs/src/blob_client.cpp b/sdk/storage/azure-storage-blobs/src/blob_client.cpp index eb5d8f431..6eaa0b986 100644 --- a/sdk/storage/azure-storage-blobs/src/blob_client.cpp +++ b/sdk/storage/azure-storage-blobs/src/blob_client.cpp @@ -262,7 +262,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (static_cast(blobRangeSize) > bufferSize) { - throw std::runtime_error( + throw Azure::Core::RequestFailedException( "buffer is not big enough, blob range size is " + std::to_string(blobRangeSize)); } @@ -270,7 +270,7 @@ namespace Azure { namespace Storage { namespace Blobs { firstChunkOptions.Context, *(firstChunk->BodyStream), buffer, firstChunkLength); if (bytesRead != firstChunkLength) { - throw std::runtime_error("error when reading body stream"); + throw Azure::Core::RequestFailedException("error when reading body stream"); } firstChunk->BodyStream.reset(); @@ -308,7 +308,7 @@ namespace Azure { namespace Storage { namespace Blobs { chunkOptions.Range.GetValue().Length.GetValue()); if (bytesRead != chunkOptions.Range.GetValue().Length.GetValue()) { - throw std::runtime_error("error when reading body stream"); + throw Azure::Core::RequestFailedException("error when reading body stream"); } if (chunkId == numChunks - 1) @@ -402,7 +402,7 @@ namespace Azure { namespace Storage { namespace Blobs { = Azure::Core::Http::BodyStream::ReadToCount(context, stream, buffer.data(), readSize); if (bytesRead != readSize) { - throw std::runtime_error("error when reading body stream"); + throw Azure::Core::RequestFailedException("error when reading body stream"); } fileWriter.Write(buffer.data(), bytesRead, offset); length -= bytesRead; diff --git a/sdk/storage/azure-storage-blobs/src/blob_container_client.cpp b/sdk/storage/azure-storage-blobs/src/blob_container_client.cpp index 879ab5379..5c152655b 100644 --- a/sdk/storage/azure-storage-blobs/src/blob_container_client.cpp +++ b/sdk/storage/azure-storage-blobs/src/blob_container_client.cpp @@ -223,12 +223,6 @@ namespace Azure { namespace Storage { namespace Blobs { protocolLayerOptions.Metadata = metadata; protocolLayerOptions.LeaseId = options.AccessConditions.LeaseId; protocolLayerOptions.IfModifiedSince = options.AccessConditions.IfModifiedSince; - if (options.AccessConditions.IfUnmodifiedSince.HasValue()) - { - // Strangely enough, this operation doesn't support If-Unmodified-Since while it does support - // If-Modified-Since - throw std::runtime_error("this operation doesn't support unmodified since access condition."); - } return Details::BlobRestClient::BlobContainer::SetMetadata( options.Context, *m_pipeline, m_blobContainerUrl, protocolLayerOptions); } diff --git a/sdk/storage/azure-storage-blobs/src/blob_sas_builder.cpp b/sdk/storage/azure-storage-blobs/src/blob_sas_builder.cpp index 455c084af..3b65c8e5f 100644 --- a/sdk/storage/azure-storage-blobs/src/blob_sas_builder.cpp +++ b/sdk/storage/azure-storage-blobs/src/blob_sas_builder.cpp @@ -29,7 +29,7 @@ namespace Azure { namespace Storage { namespace Sas { } else { - throw std::runtime_error("unknown BlobSasResource value"); + throw std::invalid_argument("unknown BlobSasResource value"); } } } // namespace diff --git a/sdk/storage/azure-storage-common/inc/azure/storage/common/storage_exception.hpp b/sdk/storage/azure-storage-common/inc/azure/storage/common/storage_exception.hpp index 31edf0b14..4f2a4d31c 100644 --- a/sdk/storage/azure-storage-common/inc/azure/storage/common/storage_exception.hpp +++ b/sdk/storage/azure-storage-common/inc/azure/storage/common/storage_exception.hpp @@ -8,13 +8,14 @@ #include #include +#include #include namespace Azure { namespace Storage { - struct StorageException : public std::runtime_error + struct StorageException : public Azure::Core::RequestFailedException { - explicit StorageException(const std::string& message) : std::runtime_error(message) {} + explicit StorageException(const std::string& message) : RequestFailedException(message) {} Azure::Core::Http::HttpStatusCode StatusCode = Azure::Core::Http::HttpStatusCode::None; std::string ReasonPhrase; diff --git a/sdk/storage/azure-storage-files-datalake/src/datalake_file_system_client.cpp b/sdk/storage/azure-storage-files-datalake/src/datalake_file_system_client.cpp index 72d83e7e0..ef9175b68 100644 --- a/sdk/storage/azure-storage-files-datalake/src/datalake_file_system_client.cpp +++ b/sdk/storage/azure-storage-files-datalake/src/datalake_file_system_client.cpp @@ -300,7 +300,10 @@ namespace Azure { namespace Storage { namespace Files { namespace DataLake { Blobs::SetBlobContainerMetadataOptions blobOptions; blobOptions.Context = options.Context; blobOptions.AccessConditions.IfModifiedSince = options.AccessConditions.IfModifiedSince; - blobOptions.AccessConditions.IfUnmodifiedSince = options.AccessConditions.IfUnmodifiedSince; + if (options.AccessConditions.IfUnmodifiedSince.HasValue()) + { + std::abort(); + } auto result = m_blobContainerClient.SetMetadata(std::move(metadata), blobOptions); Models::SetDataLakeFileSystemMetadataResult ret; ret.ETag = std::move(result->ETag); diff --git a/sdk/storage/azure-storage-files-datalake/src/datalake_path_client.cpp b/sdk/storage/azure-storage-files-datalake/src/datalake_path_client.cpp index 52e2e0308..efb039918 100644 --- a/sdk/storage/azure-storage-files-datalake/src/datalake_path_client.cpp +++ b/sdk/storage/azure-storage-files-datalake/src/datalake_path_client.cpp @@ -416,7 +416,8 @@ namespace Azure { namespace Storage { namespace Files { namespace DataLake { ret.LastModified = std::move(result->LastModified); if (!acl.HasValue()) { - throw std::runtime_error("Got null value returned when getting access control."); + throw Azure::Core::RequestFailedException( + "Got null value returned when getting access control."); } ret.Acls = std::move(acl.GetValue()); if (result->Owner.HasValue()) diff --git a/sdk/storage/azure-storage-files-datalake/src/datalake_sas_builder.cpp b/sdk/storage/azure-storage-files-datalake/src/datalake_sas_builder.cpp index 58e9a5978..b296202ac 100644 --- a/sdk/storage/azure-storage-files-datalake/src/datalake_sas_builder.cpp +++ b/sdk/storage/azure-storage-files-datalake/src/datalake_sas_builder.cpp @@ -24,7 +24,7 @@ namespace Azure { namespace Storage { namespace Sas { } else { - throw std::runtime_error("unknown DataLakeSasResource value"); + throw std::invalid_argument("unknown DataLakeSasResource value"); } } } // namespace diff --git a/sdk/storage/azure-storage-files-shares/src/share_file_client.cpp b/sdk/storage/azure-storage-files-shares/src/share_file_client.cpp index 677653ed0..b3d43cf74 100644 --- a/sdk/storage/azure-storage-files-shares/src/share_file_client.cpp +++ b/sdk/storage/azure-storage-files-shares/src/share_file_client.cpp @@ -289,7 +289,8 @@ namespace Azure { namespace Storage { namespace Files { namespace Shares { auto newResponse = Download(newOptions); if (eTag != newResponse->ETag) { - throw std::runtime_error("File was changed during the download process."); + throw Azure::Core::RequestFailedException( + "File was changed during the download process."); } return std::move(Download(newOptions)->BodyStream); }; @@ -619,7 +620,7 @@ namespace Azure { namespace Storage { namespace Files { namespace Shares { if (static_cast(fileRangeSize) > bufferSize) { - throw std::runtime_error( + throw Azure::Core::RequestFailedException( "buffer is not big enough, file range size is " + std::to_string(fileRangeSize)); } @@ -627,7 +628,7 @@ namespace Azure { namespace Storage { namespace Files { namespace Shares { firstChunkOptions.Context, *(firstChunk->BodyStream), buffer, firstChunkLength); if (bytesRead != firstChunkLength) { - throw std::runtime_error("error when reading body stream"); + throw Azure::Core::RequestFailedException("error when reading body stream"); } firstChunk->BodyStream.reset(); @@ -660,7 +661,7 @@ namespace Azure { namespace Storage { namespace Files { namespace Shares { chunkOptions.Range.GetValue().Length.GetValue()); if (bytesRead != chunkOptions.Range.GetValue().Length.GetValue()) { - throw std::runtime_error("error when reading body stream"); + throw Azure::Core::RequestFailedException("error when reading body stream"); } if (chunkId == numChunks - 1) @@ -752,7 +753,7 @@ namespace Azure { namespace Storage { namespace Files { namespace Shares { = Azure::Core::Http::BodyStream::ReadToCount(context, stream, buffer.data(), readSize); if (bytesRead != readSize) { - throw std::runtime_error("error when reading body stream"); + throw Azure::Core::RequestFailedException("error when reading body stream"); } fileWriter.Write(buffer.data(), bytesRead, offset); length -= bytesRead; diff --git a/sdk/storage/azure-storage-files-shares/src/share_sas_builder.cpp b/sdk/storage/azure-storage-files-shares/src/share_sas_builder.cpp index 43be70ff9..8fdbc91c0 100644 --- a/sdk/storage/azure-storage-files-shares/src/share_sas_builder.cpp +++ b/sdk/storage/azure-storage-files-shares/src/share_sas_builder.cpp @@ -21,7 +21,7 @@ namespace Azure { namespace Storage { namespace Sas { } else { - throw std::runtime_error("unknown ShareSasResource value"); + throw std::invalid_argument("unknown ShareSasResource value"); } } } // namespace