diff options
author | Alan Somers <asomers@gmail.com> | 2021-06-06 10:43:02 -0600 |
---|---|---|
committer | Alan Somers <asomers@gmail.com> | 2021-07-07 20:49:28 -0600 |
commit | 6511d02414ec9dd4bcaa237336c5f74869e6c41a (patch) | |
tree | 61e6cde2230efb026e9ac9c6a6e786cb6ba77642 /src/sys/signal.rs | |
parent | f90033ed35af02f39d4eaf28eff85f226c10d91b (diff) | |
download | nix-6511d02414ec9dd4bcaa237336c5f74869e6c41a.zip |
Overhaul Nix's error types
For many of Nix's consumers it be convenient to easily convert a Nix
error into a std::io::Error. That's currently not possible because of
the InvalidPath, InvalidUtf8, and UnsupportedOperation types that have
no equivalent in std::io::Error.
However, very few of Nix's public APIs actually return those unusual
errors. So a more useful API would be for Nix's standard error type to
implement Into<std::io::Error>.
This commit makes Error a simple NewType around Errno. For most
functions it's a drop-in replacement. There are only three exceptions:
* clearenv now returns a bespoke error type. It was the only Nix
function whose error couldn't be cleanly mapped onto an Errno.
* sys::signal::signal now returns Error(Errno::ENOTSUP) instead of
Error::UnsupportedOperation when the user passes an incompatible
argument to `handler`.
* When a NixPath exceeds PATH_MAX, it will now return
Error(Errno::ENAMETOOLONG) instead of Error::InvalidPath.
In the latter two cases there is now some abiguity about whether the
error code was generated by Nix or by the OS. But I think the ambiguity
is worth it for the sake of being able to implement Into<io::Error>.
This commit also introduces Error::Sys() as a migration aid. Previously
that as an enum variant. Now it's a function, but it will work in many
of the same contexts as the original.
Fixes #1155
Diffstat (limited to 'src/sys/signal.rs')
-rw-r--r-- | src/sys/signal.rs | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/src/sys/signal.rs b/src/sys/signal.rs index bf3f762b..273b3521 100644 --- a/src/sys/signal.rs +++ b/src/sys/signal.rs @@ -121,7 +121,7 @@ impl FromStr for Signal { target_os = "fuchsia", target_os = "linux", target_os = "redox")))] "SIGINFO" => Signal::SIGINFO, - _ => return Err(Error::invalid_argument()), + _ => return Err(Error::from(Errno::EINVAL)), }) } } @@ -368,7 +368,7 @@ impl TryFrom<libc::c_int> for Signal { if 0 < signum && signum < NSIG { Ok(unsafe { mem::transmute(signum) }) } else { - Err(Error::invalid_argument()) + Err(Error::from(Errno::EINVAL)) } } } @@ -664,7 +664,7 @@ pub unsafe fn sigaction(signal: Signal, sigaction: &SigAction) -> Result<SigActi /// /// # Errors /// -/// Returns [`Error::UnsupportedOperation`] if `handler` is +/// Returns [`Error(Errno::EOPNOTSUPP)`] if `handler` is /// [`SigAction`][SigActionStruct]. Use [`sigaction`][SigActionFn] instead. /// /// `signal` also returns any error from `libc::signal`, such as when an attempt @@ -681,7 +681,7 @@ pub unsafe fn signal(signal: Signal, handler: SigHandler) -> Result<SigHandler> SigHandler::SigIgn => libc::signal(signal, libc::SIG_IGN), SigHandler::Handler(handler) => libc::signal(signal, handler as libc::sighandler_t), #[cfg(not(target_os = "redox"))] - SigHandler::SigAction(_) => return Err(Error::UnsupportedOperation), + SigHandler::SigAction(_) => return Err(Error::from(Errno::ENOTSUP)), }; Errno::result(res).map(|oldhandler| { match oldhandler { @@ -949,7 +949,7 @@ mod tests { #[test] fn test_from_str_invalid_value() { - let errval = Err(Error::Sys(Errno::EINVAL)); + let errval = Err(Error::from(Errno::EINVAL)); assert_eq!("NOSIGNAL".parse::<Signal>(), errval); assert_eq!("kill".parse::<Signal>(), errval); assert_eq!("9".parse::<Signal>(), errval); |