Skip to main content

adk_site/migrate/
error.rs

1//! Errors of the migration stages.
2
3use adk_core::{AdkError, ErrorCategory, ErrorComponent};
4
5use crate::error::SiteError;
6
7/// The result type of [`crate::migrate`].
8pub type Result<T> = std::result::Result<T, MigrateError>;
9
10/// An error from a migration stage (spec 3.5 ยง7.8).
11///
12/// Converts into [`AdkError`] with component `Deploy` and a `site.migrate.*` code.
13#[derive(Debug, thiserror::Error)]
14#[non_exhaustive]
15pub enum MigrateError {
16    /// A site pipeline error.
17    #[error(transparent)]
18    Site(#[from] SiteError),
19
20    /// A request failed before an answer arrived.
21    #[error("request to '{url}' failed: {message}")]
22    Http {
23        /// The URL.
24        url: String,
25        /// What failed.
26        message: String,
27    },
28
29    /// A request answered a status the stage cannot use.
30    #[error("'{url}' answered HTTP {status}")]
31    Status {
32        /// The URL.
33        url: String,
34        /// The status.
35        status: u16,
36    },
37
38    /// The egress policy refused a request before any connection was made.
39    #[error("request to '{url}' refused: {reason}")]
40    Egress {
41        /// The URL.
42        url: String,
43        /// Why.
44        reason: String,
45    },
46
47    /// The site's `robots.txt` disallows the URL for this crawler.
48    #[error("robots.txt disallows '{url}'; the crawler does not fetch it")]
49    RobotsDisallowed {
50        /// The URL.
51        url: String,
52    },
53
54    /// The step needs a proven domain (spec 3.5 A-I9).
55    #[error(
56        "{action} on '{domain}' needs proof of control of the domain (A-I9); until a DNS TXT \
57         record or the well-known token proves it, only polite public reads run"
58    )]
59    NotProven {
60        /// The domain.
61        domain: String,
62        /// The step that was refused.
63        action: String,
64    },
65
66    /// A domain claim could not be proven.
67    #[error("domain claim for '{domain}' not proven: {reason}")]
68    ClaimFailed {
69        /// The domain.
70        domain: String,
71        /// Why.
72        reason: String,
73    },
74
75    /// A credential could not be obtained or is broader than the step allows.
76    #[error("credential refused: {0}. Issue a read-scoped credential through the broker.")]
77    Credential(String),
78
79    /// A document could not be parsed.
80    #[error("could not parse {what}: {message}")]
81    Parse {
82        /// The document.
83        what: String,
84        /// The parser's message.
85        message: String,
86    },
87
88    /// The container driver failed.
89    #[error("container driver: {0}")]
90    Container(String),
91
92    /// A screenshot could not be taken.
93    #[error("screenshot failed: {0}")]
94    Screenshot(String),
95
96    /// The archive failed.
97    #[error("legacy archive: {0}")]
98    Archive(String),
99
100    /// A DNS lookup failed.
101    #[error("DNS lookup failed: {0}")]
102    Dns(String),
103
104    /// A configured limit stopped the stage.
105    #[error("limit reached: {0}")]
106    Limit(String),
107
108    /// An approval or audit service failed.
109    #[error("{0}")]
110    Service(String),
111}
112
113impl MigrateError {
114    pub(crate) fn http(url: impl std::fmt::Display, message: impl std::fmt::Display) -> Self {
115        Self::Http { url: url.to_string(), message: message.to_string() }
116    }
117
118    pub(crate) fn parse(what: impl Into<String>, message: impl std::fmt::Display) -> Self {
119        Self::Parse { what: what.into(), message: message.to_string() }
120    }
121
122    /// The stable error code, `site.migrate.<kind>`.
123    pub fn code(&self) -> &'static str {
124        match self {
125            Self::Site(error) => error.code(),
126            Self::Http { .. } => "site.migrate.http",
127            Self::Status { .. } => "site.migrate.status",
128            Self::Egress { .. } => "site.migrate.egress_refused",
129            Self::RobotsDisallowed { .. } => "site.migrate.robots_disallowed",
130            Self::NotProven { .. } => "site.migrate.not_proven",
131            Self::ClaimFailed { .. } => "site.migrate.claim_failed",
132            Self::Credential(_) => "site.migrate.credential",
133            Self::Parse { .. } => "site.migrate.parse",
134            Self::Container(_) => "site.migrate.container",
135            Self::Screenshot(_) => "site.migrate.screenshot",
136            Self::Archive(_) => "site.migrate.archive",
137            Self::Dns(_) => "site.migrate.dns",
138            Self::Limit(_) => "site.migrate.limit",
139            Self::Service(_) => "site.migrate.service",
140        }
141    }
142
143    fn category(&self) -> ErrorCategory {
144        match self {
145            Self::Site(error) => site_category(error.code()),
146            Self::Http { .. }
147            | Self::Container(_)
148            | Self::Screenshot(_)
149            | Self::Dns(_)
150            | Self::Service(_) => ErrorCategory::Unavailable,
151            Self::Status { .. } | Self::Archive(_) => ErrorCategory::Internal,
152            Self::Egress { .. }
153            | Self::RobotsDisallowed { .. }
154            | Self::NotProven { .. }
155            | Self::Credential(_) => ErrorCategory::Forbidden,
156            Self::ClaimFailed { .. } => ErrorCategory::Unauthorized,
157            Self::Parse { .. } => ErrorCategory::InvalidInput,
158            Self::Limit(_) => ErrorCategory::ResourceExhausted,
159        }
160    }
161}
162
163/// The category of a [`SiteError`] code, as `adk-site` reports it.
164fn site_category(code: &str) -> ErrorCategory {
165    match code {
166        "site.io" | "site.host" | "site.release.signing_failed" => ErrorCategory::Unavailable,
167        "site.release.server_code" | "site.gate.failed" => ErrorCategory::Forbidden,
168        "site.release.not_found" | "site.not_found" => ErrorCategory::NotFound,
169        "site.release.sequence" | "site.pipeline" => ErrorCategory::Conflict,
170        "site.digest_mismatch" | "site.log" => ErrorCategory::Internal,
171        _ => ErrorCategory::InvalidInput,
172    }
173}
174
175impl From<MigrateError> for AdkError {
176    fn from(error: MigrateError) -> Self {
177        AdkError::new(ErrorComponent::Deploy, error.category(), error.code(), error.to_string())
178    }
179}
180
181impl From<MigrateError> for SiteError {
182    fn from(error: MigrateError) -> Self {
183        match error {
184            MigrateError::Site(error) => error,
185            other => SiteError::GateFailed(other.to_string()),
186        }
187    }
188}
189
190impl From<AdkError> for MigrateError {
191    fn from(error: AdkError) -> Self {
192        Self::Service(error.to_string())
193    }
194}