From 6ee958ce86aa0b57a96090cdd0aaeef17b766f39 Mon Sep 17 00:00:00 2001 From: Cyborus Date: Thu, 18 Jan 2024 14:48:31 -0500 Subject: [PATCH] simpler error handling --- src/lib.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 47d9bc2..7854694 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,8 +26,8 @@ pub enum ForgejoError { BadStructure(#[source] serde_json::Error, String), #[error("unexpected status code {} {}", .0.as_u16(), .0.canonical_reason().unwrap_or(""))] UnexpectedStatusCode(StatusCode), - #[error("{} {}: {}", .0.as_u16(), .0.canonical_reason().unwrap_or(""), .1)] - ApiError(StatusCode, String), + #[error("{} {}{}", .0.as_u16(), .0.canonical_reason().unwrap_or(""), .1.as_ref().map(|s| format!(": {s}")).unwrap_or_default())] + ApiError(StatusCode, Option), #[error("the provided authorization was too long to accept")] AuthTooLong, } @@ -148,22 +148,21 @@ impl Forgejo { let response = self.client.execute(request).await?; match response.status() { status if status.is_success() => Ok(response), - status if status.is_client_error() => Err(ForgejoError::ApiError( - status, - response - .json::() - .await? - .message - .unwrap_or_else(|| String::from("[no message]")), - )), + status if status.is_client_error() => { + Err(ForgejoError::ApiError(status, maybe_err(response).await)) + } status => Err(ForgejoError::UnexpectedStatusCode(status)), } } } +async fn maybe_err(res: reqwest::Response) -> Option { + res.json::().await.ok().map(|e| e.message) +} + #[derive(serde::Deserialize)] struct ErrorMessage { - message: Option, + message: String, // intentionally ignored, no need for now // url: Url }