diff --git a/crates/karapace-remote/src/http.rs b/crates/karapace-remote/src/http.rs index 9fa88f9..eae42ca 100644 --- a/crates/karapace-remote/src/http.rs +++ b/crates/karapace-remote/src/http.rs @@ -56,7 +56,28 @@ impl HttpBackend { if let Some(ref token) = self.config.auth_token { req = req.header("Authorization", &format!("Bearer {token}")); } - let resp = req.call().map_err(|e| RemoteError::Http(e.to_string()))?; + let resp = match req.call() { + Ok(r) => r, + Err(ureq::Error::StatusCode(404)) => { + return Err(RemoteError::NotFound(url.to_owned())); + } + Err(ureq::Error::StatusCode(code)) => { + return Err(RemoteError::Http(format!("HTTP {code} for {url}"))); + } + Err(e) => { + return Err(RemoteError::Http(e.to_string())); + } + }; + + let status = resp.status(); + let code = status.as_u16(); + if code == 404 { + return Err(RemoteError::NotFound(url.to_owned())); + } + if code >= 400 { + return Err(RemoteError::Http(format!("HTTP {code} for {url}"))); + } + let mut reader = resp.into_body().into_reader(); let mut body = Vec::new(); reader @@ -73,8 +94,11 @@ impl HttpBackend { if let Some(ref token) = self.config.auth_token { req = req.header("Authorization", &format!("Bearer {token}")); } - let resp = req.call().map_err(|e| RemoteError::Http(e.to_string()))?; - Ok(resp.status().into()) + match req.call() { + Ok(resp) => Ok(resp.status().into()), + Err(ureq::Error::StatusCode(code)) => Ok(code), + Err(e) => Err(RemoteError::Http(e.to_string())), + } } } @@ -94,9 +118,10 @@ impl RemoteBackend for HttpBackend { fn has_blob(&self, kind: BlobKind, key: &str) -> Result { let url = self.url(kind, key); tracing::debug!("HEAD {url}"); - match self.do_head(&url) { - Ok(status) => Ok(status == 200), - Err(_) => Ok(false), + match self.do_head(&url)? { + 200 => Ok(true), + 404 => Ok(false), + code => Err(RemoteError::Http(format!("HTTP {code} for HEAD {url}"))), } } diff --git a/crates/karapace-remote/src/transfer.rs b/crates/karapace-remote/src/transfer.rs index f12c069..9d5b66b 100644 --- a/crates/karapace-remote/src/transfer.rs +++ b/crates/karapace-remote/src/transfer.rs @@ -86,8 +86,9 @@ pub fn push_env( // 7. Update registry if key provided if let Some(key) = registry_key { let mut registry = match backend.get_registry() { - Ok(data) => Registry::from_bytes(&data).unwrap_or_default(), - Err(_) => Registry::new(), + Ok(data) => Registry::from_bytes(&data)?, + Err(RemoteError::NotFound(_)) => Registry::new(), + Err(e) => return Err(e), }; registry.publish( key,