summaryrefslogtreecommitdiffstats
path: root/src/error.rs
blob: ac47fc807e2d732c4461bbac0cf6034fb7de3e13 (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
//! 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),
    /// 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())
    }

    /// True when a retry could plausibly help (network, mirror 5xx). Deterministic
    /// build failures must not auto-retry (§8.4).
    pub fn is_transient(&self) -> bool {
        matches!(self, Error::Http(_) | Error::Io(_) | Error::Transient(_))
    }
}

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