adk_site/migrate/
error.rs1use adk_core::{AdkError, ErrorCategory, ErrorComponent};
4
5use crate::error::SiteError;
6
7pub type Result<T> = std::result::Result<T, MigrateError>;
9
10#[derive(Debug, thiserror::Error)]
14#[non_exhaustive]
15pub enum MigrateError {
16 #[error(transparent)]
18 Site(#[from] SiteError),
19
20 #[error("request to '{url}' failed: {message}")]
22 Http {
23 url: String,
25 message: String,
27 },
28
29 #[error("'{url}' answered HTTP {status}")]
31 Status {
32 url: String,
34 status: u16,
36 },
37
38 #[error("request to '{url}' refused: {reason}")]
40 Egress {
41 url: String,
43 reason: String,
45 },
46
47 #[error("robots.txt disallows '{url}'; the crawler does not fetch it")]
49 RobotsDisallowed {
50 url: String,
52 },
53
54 #[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 domain: String,
62 action: String,
64 },
65
66 #[error("domain claim for '{domain}' not proven: {reason}")]
68 ClaimFailed {
69 domain: String,
71 reason: String,
73 },
74
75 #[error("credential refused: {0}. Issue a read-scoped credential through the broker.")]
77 Credential(String),
78
79 #[error("could not parse {what}: {message}")]
81 Parse {
82 what: String,
84 message: String,
86 },
87
88 #[error("container driver: {0}")]
90 Container(String),
91
92 #[error("screenshot failed: {0}")]
94 Screenshot(String),
95
96 #[error("legacy archive: {0}")]
98 Archive(String),
99
100 #[error("DNS lookup failed: {0}")]
102 Dns(String),
103
104 #[error("limit reached: {0}")]
106 Limit(String),
107
108 #[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 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
163fn 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}