Skip to content

Commit 9b2abe2

Browse files
committed
fix FailureReason context being overshadowed by CommandError
1 parent a102a1e commit 9b2abe2

File tree

1 file changed

+47
-51
lines changed

1 file changed

+47
-51
lines changed

src/runner/test.rs

Lines changed: 47 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,59 +16,53 @@ use std::collections::{BTreeSet, HashMap, HashSet};
1616
use std::io::ErrorKind;
1717

1818
fn failure_reason(err: &Error) -> FailureReason {
19-
for cause in err.chain() {
20-
if let Some(command_error) = cause.downcast_ref::<CommandError>() {
21-
match command_error {
22-
CommandError::NoOutputFor(_)
23-
| CommandError::Timeout(_)
24-
| CommandError::KillAfterTimeoutFailed(_) => return FailureReason::Timeout,
25-
CommandError::SandboxOOM => return FailureReason::OOM,
26-
CommandError::SandboxImagePullFailed(_)
27-
| CommandError::SandboxImageMissing(_)
28-
| CommandError::SandboxContainerCreate(_)
29-
| CommandError::WorkspaceNotMountedCorrectly
30-
| CommandError::InvalidDockerInspectOutput(_) => return FailureReason::Docker,
31-
32-
CommandError::ExecutionFailed { .. } | CommandError::IO(_) | _ => {}
33-
}
34-
}
35-
36-
if let Some(reason) = cause.downcast_ref::<FailureReason>() {
37-
return reason.clone();
38-
} else if let Some(CommandError::IO(io)) = cause.downcast_ref() {
39-
match io.kind() {
40-
ErrorKind::OutOfMemory => {
41-
return FailureReason::OOM;
42-
}
43-
_ => {
44-
// FIXME use ErrorKind once #![feature(io_error_more)] is stable <https://github.com/rust-lang/rust/issues/86442>
45-
#[cfg(target_os = "linux")]
46-
match io.raw_os_error() {
47-
// <https://mariadb.com/kb/en/operating-system-error-codes/#linux-error-codes>
48-
| Some(28) /* ErrorKind::StorageFull */
49-
| Some(122) /* ErrorKind::FilesystemQuotaExceeded */
50-
| Some(31) /* TooManyLinks */=> {
51-
return FailureReason::NoSpace
52-
}
53-
_ => {}
54-
}
55-
56-
#[cfg(target_os = "windows")]
57-
match io.raw_os_error() {
58-
// <https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes>
59-
| Some(39|112) /* ErrorKind::StorageFull */
60-
| Some(1295) /* ErrorKind::FilesystemQuotaExceeded */
61-
| Some(1142) /* TooManyLinks */=> {
62-
return FailureReason::NoSpace
63-
}
64-
_ => {}
19+
if let Some(reason) = err.downcast_ref::<FailureReason>() {
20+
reason.clone()
21+
} else if let Some(command_error) = err.downcast_ref::<CommandError>() {
22+
match command_error {
23+
CommandError::NoOutputFor(_)
24+
| CommandError::Timeout(_)
25+
| CommandError::KillAfterTimeoutFailed(_) => FailureReason::Timeout,
26+
CommandError::SandboxOOM => FailureReason::OOM,
27+
CommandError::SandboxImagePullFailed(_)
28+
| CommandError::SandboxImageMissing(_)
29+
| CommandError::SandboxContainerCreate(_)
30+
| CommandError::WorkspaceNotMountedCorrectly
31+
| CommandError::InvalidDockerInspectOutput(_) => FailureReason::Docker,
32+
CommandError::IO(io) => {
33+
match io.kind() {
34+
ErrorKind::OutOfMemory => FailureReason::OOM,
35+
_ => {
36+
// FIXME use ErrorKind once #![feature(io_error_more)] is stable <https://github.com/rust-lang/rust/issues/86442>
37+
#[cfg(target_os = "linux")]
38+
match io.raw_os_error() {
39+
// <https://mariadb.com/kb/en/operating-system-error-codes/#linux-error-codes>
40+
| Some(28) /* ErrorKind::StorageFull */
41+
| Some(122) /* ErrorKind::FilesystemQuotaExceeded */
42+
| Some(31) /* TooManyLinks */=> {
43+
return FailureReason::NoSpace
44+
}
45+
_ => FailureReason::Unknown
46+
}
47+
48+
#[cfg(target_os = "windows")]
49+
match io.raw_os_error() {
50+
// <https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes>
51+
| Some(39|112) /* ErrorKind::StorageFull */
52+
| Some(1295) /* ErrorKind::FilesystemQuotaExceeded */
53+
| Some(1142) /* TooManyLinks */=> {
54+
return FailureReason::NoSpace
55+
}
56+
_ => FailureReason::Unknown
57+
}
6558
}
6659
}
6760
}
61+
CommandError::ExecutionFailed { .. } | _ => FailureReason::Unknown,
6862
}
63+
} else {
64+
FailureReason::Unknown
6965
}
70-
71-
FailureReason::Unknown
7266
}
7367

7468
pub(super) fn detect_broken<T>(res: Result<T, Error>) -> Result<T, Error> {
@@ -467,10 +461,12 @@ fn is_library(target: &Target) -> bool {
467461
.all(|k| !["example", "test", "bench"].contains(&k.as_str()))
468462
}
469463

470-
471464
#[test]
472465
fn test_failure_reason() {
473-
let error : anyhow::Error = anyhow!(CommandError::IO(std::io::Error::other("Test")));
466+
let error: anyhow::Error = anyhow!(CommandError::IO(std::io::Error::other("Test")));
474467
assert_eq!(failure_reason(&error), FailureReason::Unknown);
475-
assert_eq!(failure_reason(&error.context(FailureReason::ICE)), FailureReason::ICE);
468+
assert_eq!(
469+
failure_reason(&error.context(FailureReason::ICE)),
470+
FailureReason::ICE
471+
);
476472
}

0 commit comments

Comments
 (0)