diff options
Diffstat (limited to 'src/error.rs')
-rw-r--r-- | src/error.rs | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/src/error.rs b/src/error.rs index 2954bea..23cc452 100644 --- a/src/error.rs +++ b/src/error.rs @@ -15,7 +15,7 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. use reqwest; -use std; +use std::fmt::Display; #[derive(Debug)] pub enum Error { @@ -29,12 +29,40 @@ pub enum Error { Unknown, } +impl Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::APIError(s) => write!(f, "APIError: {}", s)?, + Self::Http(e) => write!(f, "Http: {}", e.to_string())?, + Self::Io(e) => write!(f, "Io: {}", e.to_string())?, + Self::Json(e) => write!(f, "Json: {}", e.to_string())?, + Self::ParseError(e) => write!(f, "URL: {}", e.to_string())?, + Self::Qs(e) => write!(f, "Qs: {}", e.to_string())?, + Self::Unauthorized => write!(f, "unauthorized")?, + _ => write!(f, "unknown error")?, + }; + + Ok(()) + } +} + +impl std::error::Error for Error {} + impl From<reqwest::Error> for Error { fn from(e: reqwest::Error) -> Error { Error::Http(e) } } +impl From<reqwest::StatusCode> for Error { + fn from(s: reqwest::StatusCode) -> Error { + match s { + reqwest::StatusCode::UNAUTHORIZED => Error::Unauthorized, + _ => Error::Unknown, + } + } +} + impl From<std::io::Error> for Error { fn from(e: std::io::Error) -> Error { Error::Io(e) |