summaryrefslogtreecommitdiffstats
path: root/src/error.rs
blob: 342c6284126f9f8fd3b365fb5ec2f14ee37e10a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//! Error type shared across the application.

use std::fmt;

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("io: {0}")]
    Io(#[from] std::io::Error),
    #[error("json: {0}")]
    Json(#[from] serde_json::Error),
    #[error("sqlite: {0}")]
    Db(String),
    #[error("sqlite: {0}")]
    Sqlite(#[from] rusqlite::Error),
    #[error("git: {0}")]
    Git(String),
    #[error("http fetch: {0}")]
    Http(String),
    #[error("child process {cmd}: {detail}")]
    Subprocess { cmd: String, detail: String },
    /// A fail-closed assertion. Everything that guards "we build what we reviewed"
    /// raises this: recipe_hash mismatch, pkgver mismatch, missing approval.
    #[error("assertion failed: {0}")]
    FailClosed(String),
    #[error("not found: {0}")]
    NotFound(String),
    #[error("conflict: {0}")]
    Conflict(String),
    #[error("invalid: {0}")]
    Invalid(String),
    #[error("blocked: {0}")]
    Blocked(String),
    /// The sandbox pool could not fit a lease right now. Not a failure and not
    /// a retry: the build waits, the same way it waits for a dependency it
    /// publishes (§10.4). A quota is a fact about capacity, not a bug.
    #[error("sandbox pool exhausted: {0}")]
    PoolExhausted(String),
    /// The world inside the sandbox is not the world the plan approved:
    /// a pinned dependency did not land, or the closure was incomplete.
    /// Fail-closed with its own class so a test (and an operator) can tell
    /// "the sandbox lied" apart from "the sandbox broke".
    #[error("world mismatch: {0}")]
    WorldMismatch(String),
    /// A failure the outside world caused and a retry may clear: an unreachable
    /// upstream, a half-written mirror. Distinct from a deterministic failure,
    /// which must never be retried into a different answer (§8.4).
    #[error("transient: {0}")]
    Transient(String),
    #[error("{0}")]
    Other(String),
}

impl Error {
    pub fn db(s: impl fmt::Display) -> Self {
        Error::Db(s.to_string())
    }
    pub fn git(s: impl fmt::Display) -> Self {
        Error::Git(s.to_string())
    }
    pub fn fail_closed(s: impl fmt::Display) -> Self {
        Error::FailClosed(s.to_string())
    }
    pub fn not_found(s: impl fmt::Display) -> Self {
        Error::NotFound(s.to_string())
    }
    pub fn conflict(s: impl fmt::Display) -> Self {
        Error::Conflict(s.to_string())
    }
    pub fn invalid(s: impl fmt::Display) -> Self {
        Error::Invalid(s.to_string())
    }
    pub fn blocked(s: impl fmt::Display) -> Self {
        Error::Blocked(s.to_string())
    }
    pub fn other(s: impl fmt::Display) -> Self {
        Error::Other(s.to_string())
    }
    pub fn transient(s: impl fmt::Display) -> Self {
        Error::Transient(s.to_string())
    }
    pub fn pool_exhausted(s: impl fmt::Display) -> Self {
        Error::PoolExhausted(s.to_string())
    }
    pub fn world_mismatch(s: impl fmt::Display) -> Self {
        Error::WorldMismatch(s.to_string())
    }

    /// True when the sandbox pool, not the build, is the problem: the build
    /// should block and wait, not fail and not be retried into a different
    /// answer.
    pub fn is_pool_exhausted(&self) -> bool {
        matches!(self, Error::PoolExhausted(_))
    }

    /// True when a retry could plausibly help (§8.4): an unreachable upstream, a
    /// mirror that 5xx'd, a connection that reset.
    ///
    /// `Error::Io` is *not* automatically transient (m-14). `ENOSPC` and
    /// `EACCES` are `Io`, and treating them as retryable meant a full disk or a
    /// misconfigured root turned into three identical failures, a shrug, and a
    /// build that never got the operator's attention. Only the kinds that mean
    /// "the same call might work in a moment" are retried.
    pub fn is_transient(&self) -> bool {
        match self {
            Error::Http(_) | Error::Transient(_) => true,
            // A quota is capacity, not a hiccup: retrying a build that has no
            // sandbox just burns attempts. It blocks instead.
            Error::PoolExhausted(_) | Error::WorldMismatch(_) => false,
            Error::Io(e) => matches!(
                e.kind(),
                std::io::ErrorKind::WouldBlock
                    | std::io::ErrorKind::TimedOut
                    | std::io::ErrorKind::Interrupted
                    | std::io::ErrorKind::BrokenPipe
                    | std::io::ErrorKind::ConnectionReset
                    | std::io::ErrorKind::ConnectionRefused
                    | std::io::ErrorKind::ConnectionAborted
            ),
            _ => false,
        }
    }
}

pub type Result<T> = std::result::Result<T, Error>;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_full_disk_is_not_a_retryable_failure() {
        // m-14: `Error::Io` used to mean "retry", so ENOSPC and EACCES — both
        // plain `Io` — turned into three identical failures and a build that
        // never announced itself as broken.
        let enospc = Error::Io(std::io::Error::from_raw_os_error(28));
        let eacces = Error::Io(std::io::Error::from_raw_os_error(13));
        assert!(!enospc.is_transient(), "a full disk does not clear itself between retries");
        assert!(!eacces.is_transient(), "a permissions problem does not clear itself either");
    }

    #[test]
    fn what_a_moment_might_fix_is_retried() {
        let refused = Error::Io(std::io::Error::from_raw_os_error(111));
        assert!(refused.is_transient());
        assert!(Error::transient("mirror returned 502").is_transient());
        assert!(Error::Http("connection reset".into()).is_transient());
        assert!(!Error::invalid("recipe has no pkgbase").is_transient());
        assert!(!Error::fail_closed("identity hash moved").is_transient());
        // A full pool waits; it does not retry. A lying world fails closed.
        assert!(!Error::pool_exhausted("at quota").is_transient());
        assert!(!Error::world_mismatch("missing pinned dep").is_transient());
        assert!(Error::pool_exhausted("at quota").is_pool_exhausted());
    }
}