verctl_core/lookup.rs
1//! Lookups that ask something outside the commit for its state: GitHub, a
2//! registry, or history a shallow clone lacks.
3
4use std::error::Error;
5use std::fmt;
6
7/// A lookup that could not run. Three cases qualify:
8///
9/// - The walk reached a shallow clone's boundary.
10/// - The repository names no GitHub repository.
11/// - GitHub or a registry gave no answer: a failed connection, a timeout, a
12/// 5xx, or a rate limit (see [`no_answer`]).
13///
14/// Such an error shows nothing about the release itself, so `status` reports
15/// the check as not run. Any other error stays an error. Examples are a
16/// release tag on the wrong commit, a 401 from a bad token, and a config that
17/// does not resolve. The module that makes the request marks it. Its caller
18/// never does.
19///
20/// It displays as the error it wraps and continues that error's chain, so
21/// marking an error changes no message.
22#[derive(Debug)]
23pub struct Unavailable(anyhow::Error);
24
25impl fmt::Display for Unavailable {
26 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27 write!(f, "{}", self.0)
28 }
29}
30
31impl Error for Unavailable {
32 fn source(&self) -> Option<&(dyn Error + 'static)> {
33 self.0.source()
34 }
35}
36
37/// Mark `error` as a lookup that could not run.
38#[must_use]
39pub fn unavailable(error: anyhow::Error) -> anyhow::Error {
40 anyhow::Error::new(Unavailable(error))
41}
42
43/// Whether an HTTP answer with `status` shows only that the server cannot
44/// answer now: a 5xx, a 429, or a 403 whose `x-ratelimit-remaining` header
45/// value is `0`. GitHub answers its primary rate limit with that 403 or a
46/// 429, as its [rate limit docs] describe; any other 403 is a refusal.
47///
48/// [rate limit docs]: https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api#exceeding-the-primary-rate-limit
49#[must_use]
50pub fn no_answer(status: u16, rate_limit_remaining: Option<&[u8]>) -> bool {
51 (500..600).contains(&status)
52 || status == 429
53 || (status == 403 && rate_limit_remaining == Some(b"0"))
54}
55
56/// Whether `error`, or a cause under its context, is a lookup that could not
57/// run.
58#[must_use]
59pub fn is_unavailable(error: &anyhow::Error) -> bool {
60 error.chain().any(<dyn Error>::is::<Unavailable>)
61}
62
63#[cfg(test)]
64mod tests {
65 use anyhow::{Context, anyhow};
66
67 use super::{is_unavailable, unavailable};
68
69 /// Marking keeps the message and the chain, and context added later does
70 /// not hide the mark.
71 #[test]
72 fn a_marked_error_keeps_its_chain_under_context() {
73 let marked = unavailable(anyhow!("no answer").context("get release v1.0.0"));
74 let wrapped = Err::<(), _>(marked)
75 .context("read demo")
76 .expect_err("error");
77 assert!(is_unavailable(&wrapped));
78 assert_eq!(
79 format!("{wrapped:#}"),
80 "read demo: get release v1.0.0: no answer"
81 );
82 }
83
84 #[test]
85 fn an_unmarked_error_is_not_a_lookup() {
86 let error = anyhow!("tag v1.0.0 names another commit");
87 assert!(!is_unavailable(&error));
88 }
89}