Skip to content

Commit

Permalink
[x-lint] better error messages for IO errors
Browse files Browse the repository at this point in the history
Make errors easier to diagnose.

Closes: diem#3487
Approved by: bmwill
  • Loading branch information
sunshowers authored and bors-libra committed Apr 20, 2020
1 parent df84bf7 commit aa588d4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
26 changes: 16 additions & 10 deletions devtools/x-lint/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) The Libra Core Contributors
// SPDX-License-Identifier: Apache-2.0

use std::{error, fmt, io, process::ExitStatus, result};
use std::{borrow::Cow, error, fmt, io, process::ExitStatus, result};

/// Type alias for the return type for `run` methods.
pub type Result<T, E = SystemError> = result::Result<T, E>;
Expand All @@ -15,7 +15,19 @@ pub enum SystemError {
status: ExitStatus,
},
Guppy(guppy::Error),
Io(io::Error),
Io {
context: Cow<'static, str>,
err: io::Error,
},
}

impl SystemError {
pub fn io(context: impl Into<Cow<'static, str>>, err: io::Error) -> Self {
SystemError::Io {
context: context.into(),
err,
}
}
}

impl fmt::Display for SystemError {
Expand All @@ -25,7 +37,7 @@ impl fmt::Display for SystemError {
Some(code) => write!(f, "'{}' failed with exit code {}", cmd, code),
None => write!(f, "'{}' terminated by signal", cmd),
},
SystemError::Io(err) => write!(f, "IO error: {}", err),
SystemError::Io { context, .. } => write!(f, "while {}", context),
SystemError::Guppy(err) => write!(f, "guppy error: {}", err),
}
}
Expand All @@ -35,7 +47,7 @@ impl error::Error for SystemError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
SystemError::Exec { .. } => None,
SystemError::Io(err) => Some(err),
SystemError::Io { err, .. } => Some(err),
SystemError::Guppy(err) => Some(err),
}
}
Expand All @@ -46,9 +58,3 @@ impl From<guppy::Error> for SystemError {
SystemError::Guppy(err)
}
}

impl From<io::Error> for SystemError {
fn from(err: io::Error) -> Self {
SystemError::Io(err)
}
}
3 changes: 2 additions & 1 deletion devtools/x-lint/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ impl<'l> FileContext<'l> {
/// `pub(super)` is to dissuade individual linters from loading file contexts.
pub(super) fn load(self) -> Result<ContentContext<'l>> {
let full_path = self.project_ctx.full_path(self.file_path);
let content = fs::read(&full_path)?;
let content = fs::read(&full_path)
.map_err(|err| SystemError::io(format!("loading {}", full_path.display()), err))?;
Ok(ContentContext::new(self, content))
}
}
Expand Down
3 changes: 2 additions & 1 deletion devtools/x-lint/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ impl<'cfg> LintEngine<'cfg> {
// The -z causes files to not be quoted, and to be separated by \0.
.args(&["ls-files", "-z"])
.stderr(Stdio::inherit())
.output()?;
.output()
.map_err(|err| SystemError::io("running git ls-files", err))?;
if !output.status.success() {
return Err(SystemError::Exec {
cmd: "git ls-files",
Expand Down

0 comments on commit aa588d4

Please sign in to comment.