diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2024-01-12 17:27:29 +0100 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2024-01-12 18:58:19 +0100 |
commit | be04fb276aa4777f5477c579fc82f1cef9b18d65 (patch) | |
tree | 58c884d9f78245e91cb0903179bb1bac09cdfe5d /src/error.rs | |
parent | 5349a0c4320949d803df0dd8596ba8aa5497c81c (diff) | |
download | rust-zotapi-be04fb276aa4777f5477c579fc82f1cef9b18d65.tar.gz rust-zotapi-be04fb276aa4777f5477c579fc82f1cef9b18d65.tar.bz2 rust-zotapi-be04fb276aa4777f5477c579fc82f1cef9b18d65.zip |
Make channel_stream return error on auth failure
Also reenables the relevant tests.
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) |