From ab393f4c667558adf9c50b52811307c01c82214b Mon Sep 17 00:00:00 2001 From: Cyborus04 <87248184+Cyborus04@users.noreply.github.com> Date: Tue, 11 Jul 2023 14:09:17 -0400 Subject: [PATCH] better api error --- forgejo-api/src/lib.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/forgejo-api/src/lib.rs b/forgejo-api/src/lib.rs index 6c894cf..0eb1940 100644 --- a/forgejo-api/src/lib.rs +++ b/forgejo-api/src/lib.rs @@ -22,6 +22,8 @@ pub enum ForgejoError { BadStructure, #[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) } impl From for ForgejoError { @@ -101,6 +103,7 @@ impl Forgejo { let response = self.client.execute(dbg!(request)).await?; match response.status() { status if status.is_success() => Ok(response.json::().await?), + status if status.is_client_error() => Err(ForgejoError::ApiError(status, response.json::().await?.message)), status => Err(ForgejoError::UnexpectedStatusCode(status)) } } @@ -111,11 +114,19 @@ impl Forgejo { match response.status() { status if status.is_success() => Ok(Some(response.json::().await?)), StatusCode::NOT_FOUND => Ok(None), + status if status.is_client_error() => Err(ForgejoError::ApiError(status, response.json::().await?.message)), status => Err(ForgejoError::UnexpectedStatusCode(status)) } } } +#[derive(serde::Deserialize)] +struct ErrorMessage { + message: String, + // intentionally ignored, no need for now + // url: Url +} + #[derive(serde::Deserialize, Debug, PartialEq)] pub struct Repo {