Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
fix(solc): empty 'Solc error: ' message
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Sep 2, 2023
1 parent df28b2a commit 632151f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion ethers-solc/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ impl CacheEntry {
.modified()
.map_err(|err| SolcError::io(err, file.to_path_buf()))?
.duration_since(UNIX_EPOCH)
.map_err(|err| SolcError::solc(err.to_string()))?
.map_err(SolcError::msg)?
.as_millis() as u64;
Ok(last_modification_date)
}
Expand Down
14 changes: 6 additions & 8 deletions ethers-solc/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use semver::{Version, VersionReq};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{
fmt,
io::BufRead,
path::{Path, PathBuf},
process::{Command, Output, Stdio},
str::FromStr,
Expand Down Expand Up @@ -300,7 +299,7 @@ impl Solc {
pub fn find_svm_installed_version(version: impl AsRef<str>) -> Result<Option<Self>> {
let version = version.as_ref();
let solc = Self::svm_home()
.ok_or_else(|| SolcError::solc("svm home dir not found"))?
.ok_or_else(|| SolcError::msg("svm home dir not found"))?
.join(version)
.join(format!("solc-{version}"));

Expand Down Expand Up @@ -710,23 +709,22 @@ fn compile_output(output: Output) -> Result<Vec<u8>> {
if output.status.success() {
Ok(output.stdout)
} else {
Err(SolcError::solc(String::from_utf8_lossy(&output.stderr).to_string()))
Err(SolcError::solc_output(&output))
}
}

fn version_from_output(output: Output) -> Result<Version> {
if output.status.success() {
let version = output
.stdout
let stdout = String::from_utf8_lossy(&output.stdout);
let version = stdout
.lines()
.map_while(std::result::Result::ok)
.filter(|l| !l.trim().is_empty())
.last()
.ok_or_else(|| SolcError::solc("version not found in solc output"))?;
.ok_or_else(|| SolcError::msg("Version not found in Solc output"))?;
// NOTE: semver doesn't like `+` in g++ in build metadata which is invalid semver
Ok(Version::from_str(&version.trim_start_matches("Version: ").replace(".g++", ".gcc"))?)
} else {
Err(SolcError::solc(String::from_utf8_lossy(&output.stderr).to_string()))
Err(SolcError::solc_output(&output))
}
}

Expand Down
29 changes: 21 additions & 8 deletions ethers-solc/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ pub type Result<T> = std::result::Result<T, SolcError>;
/// Various error types
#[derive(Debug, Error)]
pub enum SolcError {
/// Internal solc error
#[error("Solc Error: {0}")]
SolcError(String),
/// Errors related to the Solc executable itself.
#[error("Solc exited with {0}\n{1}")]
SolcError(std::process::ExitStatus, String),
#[error("Missing pragma from solidity file")]
PragmaNotFound,
#[error("Could not find solc version locally or upstream")]
Expand Down Expand Up @@ -49,7 +49,7 @@ pub enum SolcError {
NoContracts(String),
#[error(transparent)]
PatternError(#[from] glob::PatternError),
/// General purpose message
/// General purpose message.
#[error("{0}")]
Message(String),

Expand All @@ -65,11 +65,24 @@ impl SolcError {
pub(crate) fn io(err: io::Error, path: impl Into<PathBuf>) -> Self {
SolcIoError::new(err, path).into()
}
pub(crate) fn solc(msg: impl Into<String>) -> Self {
SolcError::SolcError(msg.into())

/// Create an error from the Solc executable's output.
pub(crate) fn solc_output(output: &std::process::Output) -> Self {
let mut msg = String::from_utf8_lossy(&output.stderr);
let mut trimmed = msg.trim();
if trimmed.is_empty() {
msg = String::from_utf8_lossy(&output.stdout);
trimmed = msg.trim();
if trimmed.is_empty() {
trimmed = "<empty output>";
}
}
SolcError::SolcError(output.status, trimmed.into())
}
pub fn msg(msg: impl Into<String>) -> Self {
SolcError::Message(msg.into())

/// General purpose message.
pub fn msg(msg: impl std::fmt::Display) -> Self {
SolcError::Message(msg.to_string())
}
}

Expand Down

0 comments on commit 632151f

Please sign in to comment.