better api error
This commit is contained in:
parent
59a04601b6
commit
ab393f4c66
|
@ -22,6 +22,8 @@ pub enum ForgejoError {
|
||||||
BadStructure,
|
BadStructure,
|
||||||
#[error("unexpected status code {} {}", .0.as_u16(), .0.canonical_reason().unwrap_or(""))]
|
#[error("unexpected status code {} {}", .0.as_u16(), .0.canonical_reason().unwrap_or(""))]
|
||||||
UnexpectedStatusCode(StatusCode),
|
UnexpectedStatusCode(StatusCode),
|
||||||
|
#[error("{} {}: {}", .0.as_u16(), .0.canonical_reason().unwrap_or(""), .1)]
|
||||||
|
ApiError(StatusCode, String)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<reqwest::Error> for ForgejoError {
|
impl From<reqwest::Error> for ForgejoError {
|
||||||
|
@ -101,6 +103,7 @@ impl Forgejo {
|
||||||
let response = self.client.execute(dbg!(request)).await?;
|
let response = self.client.execute(dbg!(request)).await?;
|
||||||
match response.status() {
|
match response.status() {
|
||||||
status if status.is_success() => Ok(response.json::<T>().await?),
|
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))
|
status => Err(ForgejoError::UnexpectedStatusCode(status))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -111,11 +114,19 @@ impl Forgejo {
|
||||||
match response.status() {
|
match response.status() {
|
||||||
status if status.is_success() => Ok(Some(response.json::<T>().await?)),
|
status if status.is_success() => Ok(Some(response.json::<T>().await?)),
|
||||||
StatusCode::NOT_FOUND => Ok(None),
|
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))
|
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)]
|
#[derive(serde::Deserialize, Debug, PartialEq)]
|
||||||
pub struct Repo {
|
pub struct Repo {
|
||||||
|
|
Loading…
Reference in a new issue