mirror of
https://codeberg.org/demostf/api-client.git
synced 2026-06-03 08:34:15 +02:00
handle demo not found in get
This commit is contained in:
parent
d27188b16d
commit
65c0f8ebb4
2 changed files with 20 additions and 2 deletions
12
src/lib.rs
12
src/lib.rs
|
|
@ -10,7 +10,7 @@ use thiserror::Error;
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
#[error("Invalid base url: {0}")]
|
#[error("Invalid base url: {0}")]
|
||||||
InvalidBaseUrl(#[source] reqwest::Error),
|
InvalidBaseUrl(reqwest::Error),
|
||||||
#[error("Request failed: {0}")]
|
#[error("Request failed: {0}")]
|
||||||
Request(#[from] reqwest::Error),
|
Request(#[from] reqwest::Error),
|
||||||
#[error("Invalid page requested")]
|
#[error("Invalid page requested")]
|
||||||
|
|
@ -23,6 +23,8 @@ pub enum Error {
|
||||||
ServerError(u16),
|
ServerError(u16),
|
||||||
#[error("Invalid response: {0}")]
|
#[error("Invalid response: {0}")]
|
||||||
InvalidResponse(String),
|
InvalidResponse(String),
|
||||||
|
#[error("Demo {0} not found")]
|
||||||
|
DemoNotFound(u32),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize)]
|
#[derive(Clone, Debug, Deserialize)]
|
||||||
|
|
@ -425,7 +427,13 @@ impl ApiClient {
|
||||||
pub async fn get(&self, demo_id: u32) -> Result<Demo, Error> {
|
pub async fn get(&self, demo_id: u32) -> Result<Demo, Error> {
|
||||||
let mut url = self.base_url.clone();
|
let mut url = self.base_url.clone();
|
||||||
url.set_path(&format!("/demos/{}", demo_id));
|
url.set_path(&format!("/demos/{}", demo_id));
|
||||||
Ok(self.client.get(url).send().await?.json().await?)
|
let response = self.client.get(url).send().await?;
|
||||||
|
|
||||||
|
if response.status() == StatusCode::NOT_FOUND {
|
||||||
|
return Err(Error::DemoNotFound(demo_id));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(response.error_for_status()?.json().await?)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get user info by id
|
/// Get user info by id
|
||||||
|
|
|
||||||
|
|
@ -200,3 +200,13 @@ async fn test_set_url_invalid_key() {
|
||||||
.await;
|
.await;
|
||||||
assert!(matches!(res.unwrap_err(), Error::InvalidApiKey));
|
assert!(matches!(res.unwrap_err(), Error::InvalidApiKey));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_get_demo_not_found() {
|
||||||
|
let client = test_client().await;
|
||||||
|
|
||||||
|
assert!(matches!(
|
||||||
|
dbg!(client.get(999).await.unwrap_err()),
|
||||||
|
Error::DemoNotFound(999)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue