cached_path/error.rs
1use thiserror::Error;
2
3/// Errors that can occur during caching.
4#[derive(Error, Debug)]
5pub enum Error {
6 /// Arises when the resource looks like a local file but it doesn't exist.
7 #[error("Treated resource as local file, but file does not exist ({0})")]
8 ResourceNotFound(String),
9
10 /// Arises when the resource looks like a URL, but is invalid.
11 #[error("Unable to parse resource URL ({0})")]
12 InvalidUrl(String),
13
14 /// Arises when the cache is being used in offline mode, but it couldn't locate
15 /// any cached versions of a remote resource.
16 #[error("Offline mode is enabled but no cached versions of resouce exist ({0})")]
17 NoCachedVersions(String),
18
19 /// Arises when the cache is corrupted for some reason.
20 ///
21 /// If this error occurs, it is almost certainly the result of an external process
22 /// "messing" with the cache directory, since `cached-path` takes great care
23 /// to avoid accidental corruption on its own.
24 #[error("Cache is corrupted ({0})")]
25 CacheCorrupted(String),
26
27 /// Arises when a resource is treated as archive, but the extraction process fails.
28 #[error("Extracting archive failed ({0})")]
29 ExtractionError(String),
30
31 /// Any IO error that could arise while attempting to cache a remote resource.
32 #[error("An IO error occurred")]
33 IoError(#[from] std::io::Error),
34
35 /// An HTTP error that could occur while attempting to fetch a remote resource.
36 #[error(transparent)]
37 HttpError(#[from] reqwest::Error),
38
39 /// Raise when configuration options are invalid.
40 #[error("Configuration error ({0})")]
41 ConfigurationError(String),
42}
43
44impl Error {
45 pub(crate) fn is_retriable(&self) -> bool {
46 match self {
47 Error::HttpError(source) => {
48 if source.is_status() {
49 matches!(
50 source.status().map(|status| status.as_u16()),
51 Some(502) | Some(503) | Some(504)
52 )
53 } else {
54 source.is_timeout()
55 }
56 }
57 _ => false,
58 }
59 }
60
61 pub fn status_code(&self) -> Option<u16> {
62 if let Error::HttpError(inner) = self {
63 Some(inner.status().unwrap().as_u16())
64 } else {
65 None
66 }
67 }
68}