Skip to main content

adler_core/
error.rs

1use std::io;
2
3/// Errors produced by the Adler engine.
4///
5/// New variants are added as subsystems land. The enum is
6/// `#[non_exhaustive]` so growth is not a breaking change.
7#[derive(Debug, thiserror::Error)]
8#[non_exhaustive]
9pub enum Error {
10    /// I/O error while reading or writing local data (sites file, cache, etc.).
11    // The message omits the source detail on purpose: the `#[from]` source is
12    // surfaced by the error chain (anyhow's `{:#}`), so embedding `{0}` here
13    // would print it twice.
14    #[error("i/o error")]
15    Io(#[from] io::Error),
16
17    /// Failed to (de)serialize JSON — typically a site-definitions file.
18    #[error("json error")]
19    Json(#[from] serde_json::Error),
20
21    /// The supplied string is not a valid username.
22    ///
23    /// The original input is preserved so callers can echo it back to the
24    /// user without re-deriving it from the error message.
25    #[error("invalid username {input:?}: {reason}")]
26    InvalidUsername {
27        /// The string the caller attempted to use as a username.
28        input: String,
29        /// Human-readable failure reason.
30        reason: String,
31    },
32
33    /// A loaded site definition failed validation (bad template, empty marker, …).
34    #[error("invalid site definition: {reason}")]
35    InvalidSite {
36        /// Human-readable failure reason, including the site name when available.
37        reason: String,
38    },
39
40    /// Building the HTTP client failed (TLS init, bad config, …).
41    ///
42    /// Per-request errors do **not** surface here — they become
43    /// [`MatchKind::Uncertain`](crate::MatchKind::Uncertain) so a single
44    /// flaky site can't abort a full run.
45    #[error("http client setup failed: {message}")]
46    HttpSetup {
47        /// Underlying error description (reqwest's error chain rendered).
48        message: String,
49    },
50
51    /// Constructing a browser backend failed (no local Chrome, bad
52    /// Browserbase credentials, etc.). Per-fetch browser failures become
53    /// [`MatchKind::Uncertain`](crate::MatchKind::Uncertain) — only setup
54    /// surfaces here.
55    #[error("browser backend setup failed: {message}")]
56    BrowserSetup {
57        /// Underlying error description.
58        message: String,
59    },
60}
61
62/// Result alias used throughout the engine.
63pub type Result<T> = std::result::Result<T, Error>;
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn io_error_converts_via_from_and_keeps_source() {
71        use std::error::Error as _;
72        let io = io::Error::new(io::ErrorKind::NotFound, "missing sites file");
73        let err: Error = io.into();
74        assert!(matches!(err, Error::Io(_)));
75        // Detail lives in the source (shown by the error chain), not the
76        // top-level message — which avoids double-printing under `{:#}`.
77        assert_eq!(err.to_string(), "i/o error");
78        let source = err.source().expect("io error has a source");
79        assert!(source.to_string().contains("missing sites file"));
80    }
81
82    #[test]
83    fn json_error_converts_via_from() {
84        let parse: serde_json::Error = serde_json::from_str::<i32>("not json").unwrap_err();
85        let err: Error = parse.into();
86        assert!(matches!(err, Error::Json(_)));
87    }
88}