Add some reusable error conversions

This commit is contained in:
Tangent 128 2018-04-15 01:42:47 -04:00
parent ee82846590
commit 885681f009
2 changed files with 20 additions and 0 deletions

View file

@ -1,5 +1,7 @@
use std::error::Error;
use std::io::{ use std::io::{
Error as IoError, Error as IoError,
ErrorKind,
stdin, stdin,
Stdin Stdin
}; };
@ -8,6 +10,7 @@ use futures::{
prelude::*, prelude::*,
stream::MapErr stream::MapErr
}; };
use hyper::Error as HyperError;
use tokio_io::{ use tokio_io::{
io::AllowStdIo, io::AllowStdIo,
codec::{ codec::{
@ -28,3 +31,10 @@ pub fn stdin_stream() -> MapErr<FramedRead<AllowStdIo<Stdin>, BytesCodec>, fn(Io
FramedRead::new(AllowStdIo::new(stdin()), BytesCodec::new()) FramedRead::new(AllowStdIo::new(stdin()), BytesCodec::new())
.map_err(WebmetroError::IoError) .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()
}
}

View file

@ -17,6 +17,16 @@ pub enum WebmetroError {
Unknown(Box<Error + Send>) 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 { impl Display for WebmetroError {
fn fmt(&self, f: &mut Formatter) -> FmtResult { fn fmt(&self, f: &mut Formatter) -> FmtResult {
match self { match self {