foundry_compilers_core/
error.rs

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