Skip to content

ErrorKind::ProcessFailed and impl From<ExitStatusError> #88306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions library/std/src/io/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,45 @@ pub enum ErrorKind {
#[stable(feature = "out_of_memory_error", since = "1.54.0")]
OutOfMemory,

/// Subprocess failed.
///
/// A subprocess (eg, run by a
/// [`Command`](crate::process::Command)) failed.
///
/// Often an [`io::Error`](`crate::io::Error`) with this kind wraps an
/// [`ExitStatusError`](crate::process::ExitStatusError),
/// (perhaps via `?` and `Into`), in which case
/// [`io::Error::get_ref`](crate::io::Error::get_ref) or
/// [`std::error::Error::source`](crate::error::Error::source)
/// is the `ExitStatusError`,
/// allowing the subprocess's exit status to be obtained.
///
/// (The exit code, exit status, or wait status is generally *not*
/// available via
/// [`io::Error::raw_os_error`](crate::io::Error::raw_os_error),
/// since process exit codes are not generally the same as OS
/// error codes..)
///
/// # Example
/// ```
/// #![feature(exit_status_error)]
/// use std::process::{Command, ExitStatusError};
///
/// fn system(shellcmd: &str) -> Result<(), std::io::Error> {
/// Command::new("sh").args(&["-c",shellcmd]).status()?.exit_ok()?;
/// Ok(())
/// }
///
/// # if cfg!(unix) {
/// let err = system("exit 23").unwrap_err();
/// let exit_error: &ExitStatusError = err.get_ref().unwrap().downcast_ref().unwrap();
/// assert_eq!(err.to_string(), "process exited unsuccessfully: exit status: 23");
/// assert_eq!(exit_error.code(), Some(23));
/// # }
/// ```
#[unstable(feature = "exit_status_error", issue = "84908")]
SubprocessFailed,

// "Unusual" error kinds which do not correspond simply to (sets
// of) OS error codes, should be added just above this comment.
// `Other` and `Uncategorised` should remain at the end:
Expand Down Expand Up @@ -350,6 +389,7 @@ impl ErrorKind {
ResourceBusy => "resource busy",
StaleNetworkFileHandle => "stale network file handle",
StorageFull => "no storage space",
SubprocessFailed => "subprocess failed",
TimedOut => "timed out",
TooManyLinks => "too many links",
Uncategorized => "uncategorized error",
Expand Down
10 changes: 10 additions & 0 deletions library/std/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,9 @@ impl crate::sealed::Sealed for ExitStatusError {}
///
/// Produced by the [`.exit_ok`](ExitStatus::exit_ok) method on [`ExitStatus`].
///
/// Implements [`Into<io::Error>`], producing an [`io::Error`]
/// of kind [`Subprocessfailed`](io::ErrorKind::SubprocessFailed).
///
/// # Examples
///
/// ```
Expand Down Expand Up @@ -1636,6 +1639,13 @@ impl fmt::Display for ExitStatusError {
#[unstable(feature = "exit_status_error", issue = "84908")]
impl crate::error::Error for ExitStatusError {}

#[unstable(feature = "exit_status_error", issue = "84908")]
impl From<ExitStatusError> for io::Error {
fn from(ese: ExitStatusError) -> io::Error {
io::Error::new(io::ErrorKind::SubprocessFailed, ese)
}
}

/// This type represents the status code a process can return to its
/// parent under normal termination.
///
Expand Down
6 changes: 5 additions & 1 deletion library/std/src/process/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ fn test_process_status() {
#[test]
fn test_process_output_fail_to_start() {
match Command::new("/no-binary-by-this-name-should-exist").output() {
Err(e) => assert_eq!(e.kind(), ErrorKind::NotFound),
Err(e) => {
assert_eq!(e.kind(), ErrorKind::NotFound);
#[cfg(unix)] // Feel free to adjust/disable if this varies on some platform
assert_eq!(e.to_string(), "No such file or directory (os error 2)");
}
Ok(..) => panic!(),
}
}
Expand Down