From 885681f00950ac0f0f2282f9c5df27f890efbd12 Mon Sep 17 00:00:00 2001 From: Tangent 128 Date: Sun, 15 Apr 2018 01:42:47 -0400 Subject: [PATCH] Add some reusable error conversions --- src/commands/mod.rs | 10 ++++++++++ src/error.rs | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index a7654ce..1daf601 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -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, 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() + } +} diff --git a/src/error.rs b/src/error.rs index d0c754d..ad11148 100644 --- a/src/error.rs +++ b/src/error.rs @@ -17,6 +17,16 @@ pub enum WebmetroError { Unknown(Box) } +impl WebmetroError { + pub fn from_str(string: &str) -> WebmetroError { + string.into() + } + + pub fn from_err(err: E) -> WebmetroError { + WebmetroError::Unknown(Box::new(err)) + } +} + impl Display for WebmetroError { fn fmt(&self, f: &mut Formatter) -> FmtResult { match self {