[pbs-devel] [PATCH proxmox-backup 3/6] api: translate ENOTFOUND to 404 for downloads

Fabian Grünbichler f.gruenbichler at proxmox.com
Tue Jul 21 15:03:34 CEST 2020


and percolate the HttpError back up on the client side

Signed-off-by: Fabian Grünbichler <f.gruenbichler at proxmox.com>
---
 src/api2/helpers.rs       | 7 ++++++-
 src/client/http_client.rs | 5 +++--
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/api2/helpers.rs b/src/api2/helpers.rs
index 9dd4ecba..a3c68087 100644
--- a/src/api2/helpers.rs
+++ b/src/api2/helpers.rs
@@ -6,7 +6,12 @@ use proxmox::http_err;
 
 pub async fn create_download_response(path: PathBuf) -> Result<Response<Body>, Error> {
     let file = tokio::fs::File::open(path.clone())
-        .map_err(move |err| http_err!(BAD_REQUEST, format!("open file {:?} failed: {}", path.clone(), err)))
+        .map_err(move |err| {
+            match err.kind() {
+                std::io::ErrorKind::NotFound => http_err!(NOT_FOUND, format!("open file {:?} failed - not found", path.clone())),
+                _ => http_err!(BAD_REQUEST, format!("open file {:?} failed: {}", path.clone(), err)),
+            }
+        })
         .await?;
 
     let payload = tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new())
diff --git a/src/client/http_client.rs b/src/client/http_client.rs
index 3886c40f..a930ea56 100644
--- a/src/client/http_client.rs
+++ b/src/client/http_client.rs
@@ -16,6 +16,7 @@ use percent_encoding::percent_encode;
 use xdg::BaseDirectories;
 
 use proxmox::{
+    api::error::HttpError,
     sys::linux::tty,
     tools::{
         fs::{file_get_json, replace_file, CreateOptions},
@@ -606,7 +607,7 @@ impl HttpClient {
                 Ok(value)
             }
         } else {
-            bail!("HTTP Error {}: {}", status, text);
+            Err(Error::from(HttpError::new(status, text)))
         }
     }
 
@@ -819,7 +820,7 @@ impl H2Client {
                 bail!("got result without data property");
             }
         } else {
-            bail!("HTTP Error {}: {}", status, text);
+            Err(Error::from(HttpError::new(status, text)))
         }
     }
 
-- 
2.20.1






More information about the pbs-devel mailing list