Box the ureq errors

This commit is contained in:
Maxime Augier 2024-08-02 11:31:18 +02:00
parent 44fba534b4
commit ec3499763b

View File

@ -194,7 +194,7 @@ pub enum ApiError {
IO(#[from] io::Error), IO(#[from] io::Error),
#[error("ureq")] #[error("ureq")]
Ureq(#[from] ureq::Error), Ureq(#[source] Box<ureq::Error>),
#[error("unexpected data: {1} when processing {0}")] #[error("unexpected data: {1} when processing {0}")]
UnexpectedData(serde_json::Value, serde_json::Error), UnexpectedData(serde_json::Value, serde_json::Error),
@ -206,6 +206,12 @@ pub enum ApiError {
FormatError(#[from] chrono::ParseError) FormatError(#[from] chrono::ParseError)
} }
impl From<ureq::Error> for ApiError {
fn from(value: ureq::Error) -> Self {
ApiError::Ureq(Box::new(value))
}
}
trait JsonExplicitError { trait JsonExplicitError {
fn into_json_with_error<T: DeserializeOwned>(self) -> Result<T, ApiError>; fn into_json_with_error<T: DeserializeOwned>(self) -> Result<T, ApiError>;
} }
@ -299,7 +305,10 @@ impl Context {
fn maybe_get<T: DeserializeOwned>(&mut self, path: &str) -> Result<Option<T>, ApiError> { fn maybe_get<T: DeserializeOwned>(&mut self, path: &str) -> Result<Option<T>, ApiError> {
match self.get(path) { match self.get(path) {
Ok(r) => Ok(Some(r)), Ok(r) => Ok(Some(r)),
Err(ApiError::Ureq(ureq::Error::Status(404, _))) => Ok(None), Err(ApiError::Ureq(e)) => match &*e {
ureq::Error::Status(404, _ ) => Ok(None),
_ => Err(ApiError::Ureq(e))
},
Err(other) => Err(other) Err(other) => Err(other)
} }
} }