forked from assert-rs/assert_cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_error.rs
More file actions
44 lines (39 loc) · 1.28 KB
/
cli_error.rs
File metadata and controls
44 lines (39 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use std::error::Error;
use std::fmt;
use diff::render as render_diff;
use std::process::Output;
use difference::Difference;
pub enum CliError {
NoSuccess(Output),
OutputMissmatch(Vec<Difference>),
}
impl fmt::Display for CliError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl fmt::Debug for CliError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
CliError::NoSuccess(ref output) => write!(f,
"Non-success error code {code:?} with this stderr:\n{stderr}",
code = output.status.code(),
stderr = String::from_utf8_lossy(&output.stderr)),
CliError::OutputMissmatch(ref diff) => {
let diff = match render_diff(&diff) {
Ok(diff) => diff,
Err(_) => return Err(fmt::Error),
};
write!(f, "Output was not as expected:\n{}", diff)
}
}
}
}
impl Error for CliError {
fn description(&self) -> &str {
match *self {
CliError::NoSuccess(_) => "Command return non-success error code.",
CliError::OutputMissmatch(_) => "Command output was not as expected.",
}
}
}