rory: handle errors from api

This commit is contained in:
seth 2023-12-08 11:09:40 -05:00
parent 7e96bced41
commit 026d4cb607
No known key found for this signature in database
GPG key ID: D31BD0D494BBEE86
5 changed files with 31 additions and 13 deletions

View file

@ -1,6 +1,6 @@
use crate::api::REQWEST_CLIENT;
use color_eyre::eyre::{eyre, Result};
use color_eyre::eyre::{eyre, Context, Result};
use log::*;
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
@ -9,6 +9,7 @@ use serde::{Deserialize, Serialize};
pub struct RoryResponse {
pub id: u64,
pub url: String,
pub error: Option<String>,
}
const RORY: &str = "https://rory.cat";
@ -25,14 +26,22 @@ pub async fn get_rory(id: Option<u64>) -> Result<RoryResponse> {
let req = REQWEST_CLIENT
.get(format!("{RORY}{ENDPOINT}/{target}"))
.build()?;
.build()
.wrap_err_with(|| "Couldn't build reqwest client!")?;
info!("Making request to {}", req.url());
let resp = REQWEST_CLIENT.execute(req).await?;
let resp = REQWEST_CLIENT
.execute(req)
.await
.wrap_err_with(|| "Couldn't make request for shiggy!")?;
let status = resp.status();
if let StatusCode::OK = status {
let data = resp.json::<RoryResponse>().await?;
let data = resp
.json::<RoryResponse>()
.await
.wrap_err_with(|| "Couldn't parse the shiggy response!")?;
Ok(data)
} else {
Err(eyre!(