diff options
Diffstat (limited to 'src/error.rs')
| -rw-r--r-- | src/error.rs | 55 |
1 files changed, 14 insertions, 41 deletions
diff --git a/src/error.rs b/src/error.rs index d7bbfaf..3e05d2a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,8 +1,8 @@ +use reqwest::{Error as ReqwestError, Url}; +use serde_json::Error as JsonError; use std::fmt; use std::io; use std::path::PathBuf; -use reqwest::Error as ReqwestError; -use serde_json::Error as JsonError; pub type AppResult<T> = Result<T, AppError>; #[derive(Debug)] @@ -10,38 +10,8 @@ pub enum AppError { FileNotFound(PathBuf), IoError(io::Error), ConfigParseError { source: JsonError, path: PathBuf }, - RequestFailed { url: String, source: ReqwestError }, - InvalidResponse { url: String, reason: String }, - Timeout { url: String }, -} - -impl AppError { - pub fn with_path(self, path: impl Into<PathBuf>) -> Self { - match self { - Self::ConfigParseError { source, .. } => Self::ConfigParseError { - source, - path: path.into(), - }, - err => err, - } - } - - pub fn with_url(self, url: impl Into<String>) -> Self { - match self { - Self::RequestFailed { source, .. } => Self::RequestFailed { - source, - url: url.into(), - }, - Self::InvalidResponse { reason, .. } => Self::InvalidResponse { - reason, - url: url.into(), - }, - Self::Timeout { .. } => Self::Timeout { - url: url.into(), - }, - err => err, - } - } + RequestFailed { url: Url, source: ReqwestError }, + InvalidResponse { url: Url, reason: String }, } impl fmt::Display for AppError { @@ -49,12 +19,13 @@ impl fmt::Display for AppError { match self { Self::FileNotFound(path) => write!(f, "File not found: {}", path.display()), Self::IoError(err) => write!(f, "I/O error: {}", err), - Self::ConfigParseError { path, .. } => write!(f, "Failed to parse config at {}", path.display()), + Self::ConfigParseError { path, .. } => { + write!(f, "Failed to parse config at {}", path.display()) + } Self::RequestFailed { url, .. } => write!(f, "Request to {} failed", url), Self::InvalidResponse { url, reason } => { write!(f, "Invalid response from {}: {}", url, reason) } - Self::Timeout { url } => write!(f, "Request to {} timed out", url), } } } @@ -79,10 +50,12 @@ impl From<io::Error> for AppError { impl From<ReqwestError> for AppError { fn from(err: ReqwestError) -> Self { - Self::RequestFailed { - url: err.url().map_or_else(|| "unknown".to_string(), |u| u.to_string()), - source: err, - } + let url = match err.url() { + Some(url) => url.clone(), + None => Url::parse("http://unknown.url").unwrap(), + }; + + Self::RequestFailed { url, source: err } } } @@ -90,7 +63,7 @@ impl From<JsonError> for AppError { fn from(err: JsonError) -> Self { Self::ConfigParseError { source: err, - path: PathBuf::from("unknown"), // Default path + path: PathBuf::from("unknown"), // Default path } } } |
