ErrorExt: trap convenience method

This commit is contained in:
Pat Hickey
2021-01-25 12:14:27 -08:00
parent 634e911a4b
commit 957c434b67
2 changed files with 9 additions and 5 deletions

View File

@@ -1,4 +1,4 @@
use anyhow::{anyhow, Context}; use anyhow::Context;
use std::ops::Deref; use std::ops::Deref;
use std::os::windows::io::{AsRawHandle, RawHandle}; use std::os::windows::io::{AsRawHandle, RawHandle};
use std::sync::mpsc::{self, Receiver, RecvTimeoutError, Sender, TryRecvError}; use std::sync::mpsc::{self, Receiver, RecvTimeoutError, Sender, TryRecvError};
@@ -69,7 +69,7 @@ impl WasiSched for SyncSched {
}; };
let state = STDIN_POLL let state = STDIN_POLL
.lock() .lock()
.map_err(|_| anyhow!("failed to take lock of STDIN_POLL"))? .map_err(|_| Error::trap("failed to take lock of STDIN_POLL"))?
.poll(waitmode)?; .poll(waitmode)?;
for readsub in stdin_read_subs.into_iter() { for readsub in stdin_read_subs.into_iter() {
match state { match state {
@@ -206,7 +206,7 @@ impl StdinPoll {
// Clean up possibly unread result from previous poll. // Clean up possibly unread result from previous poll.
Ok(_) | Err(TryRecvError::Empty) => {} Ok(_) | Err(TryRecvError::Empty) => {}
Err(TryRecvError::Disconnected) => { Err(TryRecvError::Disconnected) => {
return Err(anyhow!("StdinPoll notify_rx channel closed")) return Err(Error::trap("StdinPoll notify_rx channel closed"))
} }
} }
@@ -221,7 +221,7 @@ impl StdinPoll {
Ok(r) => Ok(r), Ok(r) => Ok(r),
Err(RecvTimeoutError::Timeout) => Ok(PollState::TimedOut), Err(RecvTimeoutError::Timeout) => Ok(PollState::TimedOut),
Err(RecvTimeoutError::Disconnected) => { Err(RecvTimeoutError::Disconnected) => {
Err(anyhow!("StdinPoll notify_rx channel closed")) Err(Error::trap("StdinPoll notify_rx channel closed"))
} }
}, },
WaitMode::Infinite => self WaitMode::Infinite => self
@@ -232,7 +232,7 @@ impl StdinPoll {
Ok(r) => Ok(r), Ok(r) => Ok(r),
Err(TryRecvError::Empty) => Ok(PollState::NotReady), Err(TryRecvError::Empty) => Ok(PollState::NotReady),
Err(TryRecvError::Disconnected) => { Err(TryRecvError::Disconnected) => {
Err(anyhow!("StdinPoll notify_rx channel closed")) Err(Error::trap("StdinPoll notify_rx channel closed"))
} }
}, },
} }

View File

@@ -48,6 +48,7 @@ pub enum ErrorKind {
} }
pub trait ErrorExt { pub trait ErrorExt {
fn trap(msg: impl Into<String>) -> Self;
fn too_big() -> Self; fn too_big() -> Self;
fn badf() -> Self; fn badf() -> Self;
fn exist() -> Self; fn exist() -> Self;
@@ -64,6 +65,9 @@ pub trait ErrorExt {
} }
impl ErrorExt for Error { impl ErrorExt for Error {
fn trap(msg: impl Into<String>) -> Self {
anyhow::anyhow!(msg.into())
}
fn too_big() -> Self { fn too_big() -> Self {
ErrorKind::TooBig.into() ErrorKind::TooBig.into()
} }