From 667878c1e186907b57b22b07b9f6d0e3ef76252a Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 16 Jul 2020 02:55:33 -0400 Subject: improve error handling a bit --- src/command.rs | 2 +- src/error.rs | 9 ++++++--- src/pty.rs | 13 ++++++++----- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/command.rs b/src/command.rs index e11df57..d02df2d 100644 --- a/src/command.rs +++ b/src/command.rs @@ -30,7 +30,7 @@ impl Command for std::process::Command { }); } - let child = self.spawn()?; + let child = self.spawn().map_err(Error::Spawn)?; Ok(Child { child, pty }) } diff --git a/src/error.rs b/src/error.rs index 5b836de..b414cf9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,10 +1,13 @@ #[derive(thiserror::Error, Debug)] pub enum Error { #[error("error creating pty")] - CreatePtyMaster(#[from] nix::Error), + CreatePty(#[source] nix::Error), - #[error("error creating pty")] - CreatePtySlave(#[from] std::io::Error), + #[error("error opening pts at {0}")] + OpenPts(std::path::PathBuf, #[source] std::io::Error), + + #[error("error spawning subprocess")] + Spawn(#[source] std::io::Error), } pub type Result = std::result::Result; diff --git a/src/pty.rs b/src/pty.rs index f0ebcd3..d76aa6b 100644 --- a/src/pty.rs +++ b/src/pty.rs @@ -11,11 +11,13 @@ impl Pty { pub fn new() -> Result { let pt = nix::pty::posix_openpt( nix::fcntl::OFlag::O_RDWR | nix::fcntl::OFlag::O_NOCTTY, - )?; - nix::pty::grantpt(&pt)?; - nix::pty::unlockpt(&pt)?; + ) + .map_err(Error::CreatePty)?; + nix::pty::grantpt(&pt).map_err(Error::CreatePty)?; + nix::pty::unlockpt(&pt).map_err(Error::CreatePty)?; - let ptsname = nix::pty::ptsname_r(&pt)?.into(); + let ptsname = + nix::pty::ptsname_r(&pt).map_err(Error::CreatePty)?.into(); let pt_fd = pt.into_raw_fd(); let pt = unsafe { std::fs::File::from_raw_fd(pt_fd) }; @@ -31,6 +33,7 @@ impl Pty { Ok(std::fs::OpenOptions::new() .read(true) .write(true) - .open(&self.ptsname)?) + .open(&self.ptsname) + .map_err(|e| Error::OpenPts(self.ptsname.clone(), e))?) } } -- cgit v1.2.3-54-g00ecf