thunderstore_engine/
error.rs1#![deny(missing_docs)]
2
3use std::fmt;
4use std::io;
5use std::num::ParseIntError;
6use std::sync::Arc;
7use thiserror::Error;
8
9pub type Result<T> = std::result::Result<T, Error>;
11
12#[derive(Error, Debug, Clone)]
23#[non_exhaustive]
24pub enum Error {
25 #[error("IO error: {0}")]
27 Io(#[source] Shared<io::Error>),
28
29 #[cfg(feature = "io")]
31 #[error("HTTP error: {0}")]
32 Http(#[source] Shared<reqwest::Error>),
33
34 #[error("JSON error: {0}")]
36 Json(#[source] Shared<serde_json::Error>),
37
38 #[cfg(feature = "io")]
40 #[error("ZIP error: {0}")]
41 Zip(#[source] Shared<zip::result::ZipError>),
42
43 #[cfg(feature = "io")]
45 #[error("Header error: {0}")]
46 Header(#[source] Shared<reqwest::header::ToStrError>),
47
48 #[error("Parse integer error: {0}")]
50 ParseInt(#[from] ParseIntError),
51
52 #[error("Missing header: {0}")]
54 MissingHeader(String),
55
56 #[error("Configuration error: {0}")]
58 Config(String),
59
60 #[error("manifest parse error: {0}")]
62 ManifestParse(String),
63
64 #[error("cache error: {0}")]
71 Cache(String),
72
73 #[error("{context} exceeds size limit of {limit} bytes")]
80 SizeLimitExceeded {
81 context: String,
83 limit: u64,
85 actual: Option<u64>,
87 },
88
89 #[error("incomplete download: expected {expected} bytes, received {actual}")]
91 IncompleteDownload {
92 expected: u64,
94 actual: u64,
96 },
97
98 #[error("unsafe name: {0}")]
100 UnsafeName(String),
101
102 #[error("unsupported cache format version {found} (expected {expected})")]
105 UnsupportedCacheVersion {
106 found: u32,
108 expected: u32,
110 },
111
112 #[error("Ecosystem error: {0}")]
114 Ecosystem(String),
115
116 #[error("Install error: {0}")]
118 Install(String),
119
120 #[error("Profile error: {0}")]
123 Profile(String),
124
125 #[error("Profile error: {0}")]
134 BadProfile(#[from] crate::profile::layout::BadProfile),
135
136 #[error("Profile error: {package} is not installed")]
138 NotInstalled {
139 package: String,
141 },
142
143 #[error("Profile error: cannot install {package}: could not resolve {}", .unresolved.join(", "))]
146 UnresolvedDependency {
147 package: String,
149 unresolved: Vec<String>,
151 },
152
153 #[error("Profile error: cannot install {package}-{version}: {package} has no version {version}")]
155 MissingVersion {
156 package: String,
158 version: String,
160 },
161
162 #[error("Profile error: unknown game {game:?}")]
164 UnknownGame {
165 game: String,
167 },
168
169 #[error("Profile error: no ecosystem profile for game {game:?}")]
172 NoGameProfile {
173 game: String,
175 },
176
177 #[error("Profile error: no {loader:?} mod loader is installed")]
180 NoModLoader {
181 loader: crate::ecosystem::LoaderKind,
183 },
184
185 #[error("Profile error: the game folder {} does not exist", .path.display())]
187 MissingGameDir {
188 path: std::path::PathBuf,
190 },
191
192 #[error("{} of {total} downloads failed: {}", .failures.len(), list_failures(.failures))]
194 Download {
195 total: usize,
197 failures: Vec<(String, Error)>,
199 },
200
201 #[error(
205 "Profile error: {}; the import is unfinished (its config files were not restored)",
206 list_import_failures(.failures)
207 )]
208 ImportUnfinished {
209 failures: Vec<(String, Error)>,
211 },
212
213 #[error("invalid header: {0}")]
216 InvalidHeader(String),
217
218 #[error("Archive extraction limit exceeded: {0}")]
221 ExtractLimitExceeded(String),
222
223 #[error("CRC32 verification failed for {path}: expected {expected:08x}, got {actual:08x}")]
225 ChecksumMismatch {
226 path: String,
228 expected: u32,
230 actual: u32,
232 },
233}
234
235fn list_failures(failures: &[(String, Error)]) -> String {
237 failures
238 .iter()
239 .map(|(name, error)| format!("{name}: {error}"))
240 .collect::<Vec<_>>()
241 .join("; ")
242}
243
244#[derive(Debug)]
259pub struct Shared<T>(Arc<T>);
260
261impl<T> Clone for Shared<T> {
262 fn clone(&self) -> Self {
263 Self(Arc::clone(&self.0))
264 }
265}
266
267impl<T> std::ops::Deref for Shared<T> {
268 type Target = T;
269
270 fn deref(&self) -> &T {
271 &self.0
272 }
273}
274
275impl<T: fmt::Display> fmt::Display for Shared<T> {
276 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
277 self.0.fmt(f)
278 }
279}
280
281fn list_import_failures(failures: &[(String, Error)]) -> String {
283 failures
284 .iter()
285 .map(|(name, error)| format!("importing {name}: {error}"))
286 .collect::<Vec<_>>()
287 .join("; ")
288}
289
290impl From<io::Error> for Error {
291 fn from(error: io::Error) -> Self {
292 Self::Io(Shared(Arc::new(error)))
293 }
294}
295
296#[cfg(feature = "io")]
297impl From<reqwest::Error> for Error {
298 fn from(error: reqwest::Error) -> Self {
299 Self::Http(Shared(Arc::new(error)))
300 }
301}
302
303impl From<serde_json::Error> for Error {
304 fn from(error: serde_json::Error) -> Self {
305 Self::Json(Shared(Arc::new(error)))
306 }
307}
308
309#[cfg(feature = "io")]
310impl From<zip::result::ZipError> for Error {
311 fn from(error: zip::result::ZipError) -> Self {
312 Self::Zip(Shared(Arc::new(error)))
313 }
314}
315
316#[cfg(feature = "io")]
317impl From<reqwest::header::ToStrError> for Error {
318 fn from(error: reqwest::header::ToStrError) -> Self {
319 Self::Header(Shared(Arc::new(error)))
320 }
321}
322
323#[cfg(test)]
324mod tests {
325 use super::*;
326
327 #[test]
328 fn profile_error_displays_message() {
329 let err = Error::Profile("bad profile".to_string());
330
331 assert_eq!(err.to_string(), "Profile error: bad profile");
332 }
333
334 #[test]
335 fn a_cloned_error_keeps_its_message_and_source() {
336 let err = Error::from(io::Error::new(io::ErrorKind::NotFound, "gone"));
337
338 let kept = err.clone();
339
340 assert_eq!(kept.to_string(), "IO error: gone");
341 assert_eq!(
342 std::error::Error::source(&kept).map(|source| source.to_string()),
343 Some("gone".to_string())
344 );
345 }
346
347 #[test]
348 fn a_wrapped_source_downcasts_to_the_original_error() {
349 let io = Error::from(io::Error::new(io::ErrorKind::NotFound, "gone"));
350 let json = Error::from(serde_json::from_str::<u8>("x").unwrap_err());
351
352 let io_source =
353 std::error::Error::source(&io).and_then(|source| source.downcast_ref::<io::Error>());
354 let json_source = std::error::Error::source(&json)
355 .and_then(|source| source.downcast_ref::<serde_json::Error>());
356
357 assert_eq!(
358 io_source.map(io::Error::kind),
359 Some(io::ErrorKind::NotFound)
360 );
361 assert!(json_source.is_some());
362 }
363}