From fdf30a8b058b8ff603df4a225f5cfdc541c6b5c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arpad=20M=C3=BCller?= Date: Mon, 16 Oct 2023 23:17:12 +0200 Subject: [PATCH] Azure blobs: Simplify error conversion by addition of to_download_error --- libs/remote_storage/src/azure_blob.rs | 80 ++++++++------------------- 1 file changed, 24 insertions(+), 56 deletions(-) diff --git a/libs/remote_storage/src/azure_blob.rs b/libs/remote_storage/src/azure_blob.rs index 4f7c8ce6f427..4410a2ba308a 100644 --- a/libs/remote_storage/src/azure_blob.rs +++ b/libs/remote_storage/src/azure_blob.rs @@ -114,22 +114,7 @@ impl AzureBlobStorage { // https://github.com/neondatabase/neon/issues/5563 let mut buf = Vec::new(); while let Some(part) = response.next().await { - let part = match part { - Ok(l) => l, - Err(e) => { - return Err(if let Some(http_err) = e.as_http_error() { - match http_err.status() { - StatusCode::NotFound => DownloadError::NotFound, - StatusCode::BadRequest => { - DownloadError::BadInput(anyhow::Error::new(e)) - } - _ => DownloadError::Other(anyhow::Error::new(e)), - } - } else { - DownloadError::Other(e.into()) - }); - } - }; + let part = part.map_err(to_download_error)?; let data = part .data .collect() @@ -150,30 +135,16 @@ impl AzureBlobStorage { ) -> Result { let builder = blob_client.get_metadata(); - match builder.into_future().await { - Ok(r) => { - let mut map = HashMap::new(); - - for md in r.metadata.iter() { - map.insert( - md.name().as_str().to_string(), - md.value().as_str().to_string(), - ); - } - Ok(StorageMetadata(map)) - } - Err(e) => { - return Err(if let Some(http_err) = e.as_http_error() { - match http_err.status() { - StatusCode::NotFound => DownloadError::NotFound, - StatusCode::BadRequest => DownloadError::BadInput(anyhow::Error::new(e)), - _ => DownloadError::Other(anyhow::Error::new(e)), - } - } else { - DownloadError::Other(e.into()) - }); - } + let response = builder.into_future().await.map_err(to_download_error)?; + let mut map = HashMap::new(); + + for md in response.metadata.iter() { + map.insert( + md.name().as_str().to_string(), + md.value().as_str().to_string(), + ); } + Ok(StorageMetadata(map)) } async fn permit(&self, kind: RequestKind) -> tokio::sync::SemaphorePermit<'_> { @@ -192,6 +163,18 @@ fn to_azure_metadata(metadata: StorageMetadata) -> Metadata { res } +fn to_download_error(error: azure_core::Error) -> DownloadError { + if let Some(http_err) = error.as_http_error() { + match http_err.status() { + StatusCode::NotFound => DownloadError::NotFound, + StatusCode::BadRequest => DownloadError::BadInput(anyhow::Error::new(error)), + _ => DownloadError::Other(anyhow::Error::new(error)), + } + } else { + DownloadError::Other(error.into()) + } +} + #[async_trait::async_trait] impl RemoteStorage for AzureBlobStorage { async fn list_prefixes( @@ -226,23 +209,8 @@ impl RemoteStorage for AzureBlobStorage { let mut response = builder.into_stream(); let mut res = Vec::new(); - while let Some(l) = response.next().await { - let entry = match l { - Ok(l) => l, - Err(e) => { - return Err(if let Some(http_err) = e.as_http_error() { - match http_err.status() { - StatusCode::NotFound => DownloadError::NotFound, - StatusCode::BadRequest => { - DownloadError::BadInput(anyhow::Error::new(e)) - } - _ => DownloadError::Other(anyhow::Error::new(e)), - } - } else { - DownloadError::Other(e.into()) - }); - } - }; + while let Some(entry) = response.next().await { + let entry = entry.map_err(to_download_error)?; let name_iter = entry .blobs .prefixes()