1use std::path::PathBuf;
4
5use adk_core::{AdkError, ErrorCategory, ErrorComponent};
6
7use crate::scan::ServerCodeFinding;
8
9pub type Result<T> = std::result::Result<T, SiteError>;
11
12#[derive(Debug, thiserror::Error)]
16#[non_exhaustive]
17pub enum SiteError {
18 #[error("i/o error at '{}': {source}", path.display())]
20 Io {
21 path: PathBuf,
23 #[source]
25 source: std::io::Error,
26 },
27
28 #[error("site configuration is invalid: {0}. Fix site.toml and load the project again.")]
30 InvalidConfig(String),
31
32 #[error(
34 "content item '{path}' is invalid: {message}. Fix the item and load the project again."
35 )]
36 InvalidContent {
37 path: String,
39 message: String,
41 },
42
43 #[error("template '{template}' failed: {message}. Check the theme's templates.")]
45 Template {
46 template: String,
48 message: String,
50 },
51
52 #[error(
55 "release refused: {} server-executable file(s) or server-side include directive(s) (A-I1), \
56 first '{}'. Remove them from the output and build again.",
57 findings.len(),
58 findings.first().map_or("", |f| f.path.as_str())
59 )]
60 ServerCode {
61 findings: Vec<ServerCodeFinding>,
63 },
64
65 #[error("digest mismatch for '{path}': expected {expected}, found {actual}")]
67 DigestMismatch {
68 path: String,
70 expected: String,
72 actual: String,
74 },
75
76 #[error("release {seq} not found: {message}")]
78 ReleaseNotFound {
79 seq: u64,
81 message: String,
83 },
84
85 #[error("release sequence error: {0}")]
87 Sequence(String),
88
89 #[error("release signing failed: {0}")]
91 Signing(String),
92
93 #[error("site log error: {0}")]
95 Log(String),
96
97 #[error("host '{host}' failed: {message}")]
99 Host {
100 host: String,
102 message: String,
104 },
105
106 #[error("media '{name}' refused: {reason}")]
108 MediaRefused {
109 name: String,
111 reason: String,
113 },
114
115 #[error("launch gate failed: {0}")]
117 GateFailed(String),
118
119 #[error("publishing pipeline: {0}")]
121 Pipeline(String),
122
123 #[error("content item '{0}' not found")]
125 NotFound(String),
126
127 #[error("generator '{generator}' failed: {message}")]
129 Generator {
130 generator: String,
132 message: String,
134 },
135
136 #[error("serialisation failed for {what}: {message}")]
138 Serde {
139 what: String,
141 message: String,
143 },
144}
145
146impl SiteError {
147 pub(crate) fn io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
148 Self::Io { path: path.into(), source }
149 }
150
151 pub(crate) fn serde(what: impl Into<String>, message: impl std::fmt::Display) -> Self {
152 Self::Serde { what: what.into(), message: message.to_string() }
153 }
154
155 pub(crate) fn host(host: impl Into<String>, message: impl Into<String>) -> Self {
156 Self::Host { host: host.into(), message: message.into() }
157 }
158
159 pub fn code(&self) -> &'static str {
161 match self {
162 Self::Io { .. } => "site.io",
163 Self::InvalidConfig(_) => "site.invalid_config",
164 Self::InvalidContent { .. } => "site.invalid_content",
165 Self::Template { .. } => "site.template",
166 Self::ServerCode { .. } => "site.release.server_code",
167 Self::DigestMismatch { .. } => "site.digest_mismatch",
168 Self::ReleaseNotFound { .. } => "site.release.not_found",
169 Self::Sequence(_) => "site.release.sequence",
170 Self::Signing(_) => "site.release.signing_failed",
171 Self::Log(_) => "site.log",
172 Self::Host { .. } => "site.host",
173 Self::MediaRefused { .. } => "site.media.refused",
174 Self::GateFailed(_) => "site.gate.failed",
175 Self::Pipeline(_) => "site.pipeline",
176 Self::NotFound(_) => "site.not_found",
177 Self::Generator { .. } => "site.generator",
178 Self::Serde { .. } => "site.serde",
179 }
180 }
181
182 fn category(&self) -> ErrorCategory {
183 match self {
184 Self::Io { .. } | Self::Host { .. } | Self::Signing(_) | Self::Generator { .. } => {
185 ErrorCategory::Unavailable
186 }
187 Self::InvalidConfig(_)
188 | Self::InvalidContent { .. }
189 | Self::Template { .. }
190 | Self::MediaRefused { .. }
191 | Self::Serde { .. } => ErrorCategory::InvalidInput,
192 Self::ServerCode { .. } | Self::GateFailed(_) => ErrorCategory::Forbidden,
193 Self::DigestMismatch { .. } | Self::Log(_) => ErrorCategory::Internal,
194 Self::ReleaseNotFound { .. } | Self::NotFound(_) => ErrorCategory::NotFound,
195 Self::Sequence(_) | Self::Pipeline(_) => ErrorCategory::Conflict,
196 }
197 }
198}
199
200impl From<SiteError> for AdkError {
201 fn from(error: SiteError) -> Self {
202 AdkError::new(ErrorComponent::Deploy, error.category(), error.code(), error.to_string())
203 }
204}
205
206impl From<AdkError> for SiteError {
207 fn from(error: AdkError) -> Self {
208 Self::Log(error.to_string())
209 }
210}