diff options
author | Cyborus04 <87248184+Cyborus04@users.noreply.github.com> | 2023-07-11 20:09:17 +0200 |
---|---|---|
committer | Cyborus04 <87248184+Cyborus04@users.noreply.github.com> | 2023-07-11 20:09:17 +0200 |
commit | ab393f4c667558adf9c50b52811307c01c82214b (patch) | |
tree | f1b8a304666304655ad6f09fc0bea5b0ae8522e0 | |
parent | choose upstream name in `repo create` (diff) | |
download | forgejo-cli-ab393f4c667558adf9c50b52811307c01c82214b.tar.xz forgejo-cli-ab393f4c667558adf9c50b52811307c01c82214b.zip |
better api error
-rw-r--r-- | forgejo-api/src/lib.rs | 11 |
1 files changed, 11 insertions, 0 deletions
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<reqwest::Error> 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::<T>().await?), + status if status.is_client_error() => Err(ForgejoError::ApiError(status, response.json::<ErrorMessage>().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::<T>().await?)), StatusCode::NOT_FOUND => Ok(None), + status if status.is_client_error() => Err(ForgejoError::ApiError(status, response.json::<ErrorMessage>().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 { |