mirror of
https://github.com/marcoallegretti/karapace.git
synced 2026-03-26 13:33:09 +00:00
fix(remote): surface HTTP and registry errors
- Map HTTP 404 to RemoteError::NotFound; propagate other HTTP errors\n- Fail push when the remote registry payload is invalid
This commit is contained in:
parent
f320608175
commit
8feee8048c
2 changed files with 34 additions and 8 deletions
|
|
@ -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<bool, RemoteError> {
|
||||
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}"))),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Reference in a new issue