ethers_solc/
error.rs

1use semver::Version;
2use std::{
3    io,
4    path::{Path, PathBuf},
5};
6use thiserror::Error;
7
8pub type Result<T> = std::result::Result<T, SolcError>;
9
10/// Various error types
11#[derive(Debug, Error)]
12pub enum SolcError {
13    /// Errors related to the Solc executable itself.
14    #[error("Solc exited with {0}\n{1}")]
15    SolcError(std::process::ExitStatus, String),
16    #[error("Missing pragma from solidity file")]
17    PragmaNotFound,
18    #[error("Could not find solc version locally or upstream")]
19    VersionNotFound,
20    #[error("Checksum mismatch for {file}: expected {expected} found {detected} for {version}")]
21    ChecksumMismatch { version: Version, expected: String, detected: String, file: PathBuf },
22    #[error("Checksum not found for {version}")]
23    ChecksumNotFound { version: Version },
24    #[error(transparent)]
25    SemverError(#[from] semver::Error),
26    /// Deserialization error
27    #[error(transparent)]
28    SerdeJson(#[from] serde_json::Error),
29    /// Filesystem IO error
30    #[error(transparent)]
31    Io(#[from] SolcIoError),
32    #[error("File could not be resolved due to broken symlink: {0}.")]
33    ResolveBadSymlink(SolcIoError),
34    /// Failed to resolve a file
35    #[error("Failed to resolve file: {0}.\n Check configured remappings.")]
36    Resolve(SolcIoError),
37    #[error("File cannot be resolved due to mismatch of file name case: {error}.\n Found existing file: {existing_file:?}\n Please check the case of the import.")]
38    ResolveCaseSensitiveFileName { error: SolcIoError, existing_file: PathBuf },
39    #[error(
40        r#"{0}.
41    --> {1:?}
42        {2:?}"#
43    )]
44    FailedResolveImport(Box<SolcError>, PathBuf, PathBuf),
45    #[cfg(all(feature = "svm-solc", not(target_arch = "wasm32")))]
46    #[error(transparent)]
47    SvmError(#[from] svm::SolcVmError),
48    #[error("No contracts found at \"{0}\"")]
49    NoContracts(String),
50    #[error(transparent)]
51    PatternError(#[from] glob::PatternError),
52    /// General purpose message.
53    #[error("{0}")]
54    Message(String),
55
56    #[error("No artifact found for `{}:{}`", .0.display(), .1)]
57    ArtifactNotFound(PathBuf, String),
58
59    #[cfg(feature = "project-util")]
60    #[error(transparent)]
61    FsExtra(#[from] fs_extra::error::Error),
62}
63
64impl SolcError {
65    pub(crate) fn io(err: io::Error, path: impl Into<PathBuf>) -> Self {
66        SolcIoError::new(err, path).into()
67    }
68
69    /// Create an error from the Solc executable's output.
70    pub(crate) fn solc_output(output: &std::process::Output) -> Self {
71        let mut msg = String::from_utf8_lossy(&output.stderr);
72        let mut trimmed = msg.trim();
73        if trimmed.is_empty() {
74            msg = String::from_utf8_lossy(&output.stdout);
75            trimmed = msg.trim();
76            if trimmed.is_empty() {
77                trimmed = "<empty output>";
78            }
79        }
80        SolcError::SolcError(output.status, trimmed.into())
81    }
82
83    /// General purpose message.
84    pub fn msg(msg: impl std::fmt::Display) -> Self {
85        SolcError::Message(msg.to_string())
86    }
87}
88
89macro_rules! _format_err {
90    ($($tt:tt)*) => {
91        $crate::error::SolcError::msg(format!($($tt)*))
92    };
93}
94#[allow(unused)]
95pub(crate) use _format_err as format_err;
96
97macro_rules! _bail {
98    ($($tt:tt)*) => { return Err($crate::error::format_err!($($tt)*)) };
99}
100#[allow(unused)]
101pub(crate) use _bail as bail;
102
103#[derive(Debug, Error)]
104#[error("\"{}\": {io}", self.path.display())]
105pub struct SolcIoError {
106    io: io::Error,
107    path: PathBuf,
108}
109
110impl SolcIoError {
111    pub fn new(io: io::Error, path: impl Into<PathBuf>) -> Self {
112        Self { io, path: path.into() }
113    }
114
115    /// The path at which the error occurred
116    pub fn path(&self) -> &Path {
117        &self.path
118    }
119
120    /// The underlying `io::Error`
121    pub fn source(&self) -> &io::Error {
122        &self.io
123    }
124}
125
126impl From<SolcIoError> for io::Error {
127    fn from(err: SolcIoError) -> Self {
128        err.io
129    }
130}