chore(solc): improve io error for bad symlinks (#1594)

This commit is contained in:
Matthias Seitz 2022-08-13 23:03:48 +02:00 committed by GitHub
parent 608fd841c5
commit ad256997d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 3 deletions

View File

@ -1,5 +1,8 @@
use semver::Version;
use std::{io, path::PathBuf};
use std::{
io,
path::{Path, PathBuf},
};
use thiserror::Error;
pub type Result<T> = std::result::Result<T, SolcError>;
@ -27,6 +30,8 @@ pub enum SolcError {
/// Failed to resolve a file
#[error("Failed to resolve file: {0}.\n Check configured remappings.")]
Resolve(SolcIoError),
#[error("File could not be resolved due to broken symlink: {0}.")]
ResolveBadSymlink(SolcIoError),
#[cfg(feature = "svm-solc")]
#[error(transparent)]
SvmError(#[from] svm::SolcVmError),
@ -83,6 +88,16 @@ impl SolcIoError {
pub fn new(io: io::Error, path: impl Into<PathBuf>) -> Self {
Self { io, path: path.into() }
}
/// The path at which the error occurred
pub fn path(&self) -> &Path {
&self.path
}
/// The underlying `io::Error`
pub fn source(&self) -> &io::Error {
&self.io
}
}
impl From<SolcIoError> for io::Error {

View File

@ -818,9 +818,15 @@ pub struct Node {
impl Node {
/// Reads the content of the file and returns a [Node] containing relevant information
pub fn read(file: impl AsRef<Path>) -> crate::Result<Self> {
pub fn read(file: impl AsRef<Path>) -> Result<Self> {
let file = file.as_ref();
let source = Source::read(file).map_err(SolcError::Resolve)?;
let source = Source::read(file).map_err(|err| {
if !err.path().exists() && err.path().is_symlink() {
SolcError::ResolveBadSymlink(err)
} else {
SolcError::Resolve(err)
}
})?;
let data = SolData::parse(source.as_ref(), file);
Ok(Self { path: file.to_path_buf(), source, data })
}