Add some reusable error conversions
This commit is contained in:
parent
ee82846590
commit
885681f009
2 changed files with 20 additions and 0 deletions
|
@ -1,5 +1,7 @@
|
|||
use std::error::Error;
|
||||
use std::io::{
|
||||
Error as IoError,
|
||||
ErrorKind,
|
||||
stdin,
|
||||
Stdin
|
||||
};
|
||||
|
@ -8,6 +10,7 @@ use futures::{
|
|||
prelude::*,
|
||||
stream::MapErr
|
||||
};
|
||||
use hyper::Error as HyperError;
|
||||
use tokio_io::{
|
||||
io::AllowStdIo,
|
||||
codec::{
|
||||
|
@ -28,3 +31,10 @@ pub fn stdin_stream() -> MapErr<FramedRead<AllowStdIo<Stdin>, BytesCodec>, fn(Io
|
|||
FramedRead::new(AllowStdIo::new(stdin()), BytesCodec::new())
|
||||
.map_err(WebmetroError::IoError)
|
||||
}
|
||||
|
||||
pub fn to_hyper_error(err: WebmetroError) -> HyperError {
|
||||
match err {
|
||||
WebmetroError::IoError(io_err) => io_err.into(),
|
||||
err => IoError::new(ErrorKind::InvalidData, err.description()).into()
|
||||
}
|
||||
}
|
||||
|
|
10
src/error.rs
10
src/error.rs
|
@ -17,6 +17,16 @@ pub enum WebmetroError {
|
|||
Unknown(Box<Error + Send>)
|
||||
}
|
||||
|
||||
impl WebmetroError {
|
||||
pub fn from_str(string: &str) -> WebmetroError {
|
||||
string.into()
|
||||
}
|
||||
|
||||
pub fn from_err<E: Error + Send + 'static>(err: E) -> WebmetroError {
|
||||
WebmetroError::Unknown(Box::new(err))
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for WebmetroError {
|
||||
fn fmt(&self, f: &mut Formatter) -> FmtResult {
|
||||
match self {
|
||||
|
|
Loading…
Reference in a new issue