Skip to content

Commit def2efa

Browse files
committed
Add better error handling
(mostly because I have bad internet)
1 parent 21c1100 commit def2efa

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

src/registry.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::sync::Mutex;
22

33
use json::JsonValue;
44
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
5-
use ureq::Error;
5+
use ureq::{Error, ErrorKind};
66

77
use http_auth::parse_challenges;
88

@@ -56,8 +56,20 @@ pub fn get_latest_digest(image: &Image, token: Option<&String>, config: &JsonVal
5656
digest: None,
5757
..image.clone()
5858
}
59-
}
60-
Err(ureq::Error::Transport(e)) => error!("Failed to send request!\n{}", e),
59+
},
60+
Err(Error::Transport(error)) => {
61+
match error.kind() {
62+
ErrorKind::Dns => {
63+
warn!("Failed to lookup the IP of the registry, retrying.");
64+
return get_latest_digest(image, token, config)
65+
}, // If something goes really wrong, this can get stuck in a loop
66+
ErrorKind::ConnectionFailed => {
67+
warn!("Connection probably timed out, retrying.");
68+
return get_latest_digest(image, token, config)
69+
}, // Same here
70+
_ => error!("Failed to retrieve image digest\n{}!", error)
71+
}
72+
},
6173
};
6274
match raw_response.header("docker-content-digest") {
6375
Some(digest) => Image {
@@ -83,7 +95,7 @@ pub fn get_latest_digests(images: Vec<&Image>, token: Option<&String>, config: &
8395

8496
pub fn get_token(images: Vec<&Image>, auth_url: &str, credentials: &Option<String>) -> String {
8597
let mut final_url = auth_url.to_owned();
86-
for image in images {
98+
for image in &images {
8799
final_url = format!("{}&scope=repository:{}:pull", final_url, image.repository);
88100
}
89101
let mut base_request = ureq::get(&final_url).set("Accept", "application/vnd.oci.image.index.v1+json"); // Seems to be unnecesarry. Will probably remove in the future
@@ -99,6 +111,19 @@ pub fn get_token(images: Vec<&Image>, auth_url: &str, credentials: &Option<Strin
99111
error!("Failed to parse response into string!\n{}", e)
100112
}
101113
},
114+
Err(Error::Transport(error)) => {
115+
match error.kind() {
116+
ErrorKind::Dns => {
117+
warn!("Failed to lookup the IP of the registry, retrying.");
118+
return get_token(images, auth_url, credentials)
119+
}, // If something goes really wrong, this can get stuck in a loop
120+
ErrorKind::ConnectionFailed => {
121+
warn!("Connection probably timed out, retrying.");
122+
return get_token(images, auth_url, credentials)
123+
}, // Same here
124+
_ => error!("Token request failed\n{}!", error)
125+
}
126+
},
102127
Err(e) => {
103128
error!("Token request failed!\n{}", e)
104129
}

0 commit comments

Comments
 (0)