Skip to main content

vivacity_core/
error.rs

1use std::path::{Path, PathBuf};
2
3#[derive(Debug, thiserror::Error)]
4pub enum Error {
5    #[error("failed to read {path}: {source}")]
6    ReadFile {
7        path: PathBuf,
8        #[source]
9        source: std::io::Error,
10    },
11
12    #[error("I/O error at {path}: {source}")]
13    Io {
14        path: PathBuf,
15        #[source]
16        source: std::io::Error,
17    },
18
19    #[error("invalid JSON in {context}: {source}")]
20    Json {
21        context: String,
22        #[source]
23        source: serde_json::Error,
24    },
25
26    #[error("invalid zip archive for {dest}: {source}")]
27    Zip {
28        dest: PathBuf,
29        #[source]
30        source: zip::result::ZipError,
31    },
32
33    #[error("hostile archive refused for {dest}: {reason}")]
34    HostileArchive { dest: PathBuf, reason: String },
35
36    /// The same refusal, once the caller knows which package the archive
37    /// belongs to: the path the extractor can name is the store's own
38    /// temporary directory, which tells a reader nothing.
39    #[error("hostile archive refused for {name} ({version}): {reason}")]
40    HostileDist {
41        name: String,
42        version: String,
43        reason: String,
44    },
45
46    #[error("HTTP failure for {url}: {message}")]
47    Http { url: String, message: String },
48
49    #[error("dist checksum mismatch for {name}: expected sha1 {expected}, got {actual}")]
50    ShasumMismatch {
51        name: String,
52        expected: String,
53        actual: String,
54    },
55
56    #[error("cannot encode non-finite float ({0}) as JSON (PHP json_encode would fail too)")]
57    NonFiniteFloat(f64),
58
59    /// Plugin emulation that cannot be reproduced byte for byte, detected
60    /// before any change to vendor/: the CLI delegates to Composer.
61    #[error("{0}")]
62    Unsupported(String),
63
64    /// A refusal Composer states in its own words (`PathDownloader`):
65    /// printed as is, exit 1.
66    #[error("{0}")]
67    Refused(String),
68}
69
70impl Error {
71    pub fn io(path: &Path) -> impl FnOnce(std::io::Error) -> Error + '_ {
72        move |source| Error::Io {
73            path: path.to_path_buf(),
74            source,
75        }
76    }
77
78    pub fn zip(dest: &Path) -> impl FnOnce(zip::result::ZipError) -> Error + '_ {
79        move |source| Error::Zip {
80            dest: dest.to_path_buf(),
81            source,
82        }
83    }
84}
85
86pub type Result<T> = std::result::Result<T, Error>;