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
40impl Error {
41 pub(crate) fn is_retriable(&self) -> bool {
42 match self {
43 Error::HttpError(source) => {
44 if source.is_status() {
45 matches!(
46 source.status().map(|status| status.as_u16()),
47 Some(502) | Some(503) | Some(504)
48 )
49 } else {
50 source.is_timeout()
51 }
52 }
53 _ => false,
54 }
55 }
56
57 pub fn status_code(&self) -> Option<u16> {
58 if let Error::HttpError(inner) = self {
59 Some(inner.status().unwrap().as_u16())
60 } else {
61 None
62 }
63 }
64}