Skip to main content

deputy_core/
error.rs

1use std::fmt;
2
3/// Convenience alias for fallible Deputy operations.
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// The error surface shared across Deputy. Kept small and `#[non_exhaustive]` so new
7/// variants can be added without a breaking change.
8#[derive(Debug, Clone, PartialEq, Eq)]
9#[non_exhaustive]
10pub enum Error {
11    /// An artifact was asked to move along an edge the state machine does not permit.
12    /// See `docs/PIPELINE.md` ยง7.
13    IllegalTransition {
14        from: &'static str,
15        to: &'static str,
16    },
17    /// A downloaded artifact's content hash did not match its pinned, expected hash.
18    Integrity { expected: String, actual: String },
19    /// A referenced entity (artifact, repo, record) was not found.
20    NotFound { what: String },
21    /// The current actor lacks a valid mID session for a privileged operation.
22    Unauthorized,
23    /// Input could not be parsed into a well-formed domain value.
24    Malformed { what: String },
25    /// A lower layer (storage, database, crypto, I/O) failed. Carries a human-readable
26    /// detail; the originating crate keeps the richer typed error.
27    Backend { detail: String },
28}
29
30impl fmt::Display for Error {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            Error::IllegalTransition { from, to } => {
34                write!(f, "illegal state transition: {from} -> {to}")
35            }
36            Error::Integrity { expected, actual } => {
37                write!(
38                    f,
39                    "integrity check failed: expected {expected}, got {actual}"
40                )
41            }
42            Error::NotFound { what } => write!(f, "not found: {what}"),
43            Error::Unauthorized => f.write_str("unauthorized: a verified mID session is required"),
44            Error::Malformed { what } => write!(f, "malformed input: {what}"),
45            Error::Backend { detail } => write!(f, "backend error: {detail}"),
46        }
47    }
48}
49
50impl std::error::Error for Error {}