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    #[error("HTTP failure for {url}: {message}")]
37    Http { url: String, message: String },
38
39    #[error("dist checksum mismatch for {name}: expected sha1 {expected}, got {actual}")]
40    ShasumMismatch {
41        name: String,
42        expected: String,
43        actual: String,
44    },
45
46    #[error("cannot encode non-finite float ({0}) as JSON (PHP json_encode would fail too)")]
47    NonFiniteFloat(f64),
48
49    /// Plugin emulation that cannot be reproduced byte for byte, detected
50    /// before any change to vendor/: the CLI delegates to Composer.
51    #[error("{0}")]
52    Unsupported(String),
53
54    /// A refusal Composer states in its own words (`PathDownloader`):
55    /// printed as is, exit 1.
56    #[error("{0}")]
57    Refused(String),
58}
59
60impl Error {
61    pub fn io(path: &Path) -> impl FnOnce(std::io::Error) -> Error + '_ {
62        move |source| Error::Io {
63            path: path.to_path_buf(),
64            source,
65        }
66    }
67
68    pub fn zip(dest: &Path) -> impl FnOnce(zip::result::ZipError) -> Error + '_ {
69        move |source| Error::Zip {
70            dest: dest.to_path_buf(),
71            source,
72        }
73    }
74}
75
76pub type Result<T> = std::result::Result<T, Error>;