From 86318aa4aa8a7547e6398342a0c7b609e307dd5d Mon Sep 17 00:00:00 2001 From: seth Date: Thu, 16 Nov 2023 23:19:15 -0500 Subject: chore: improve error handling and logging for api requests --- src/api/guzzle.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'src/api/guzzle.rs') diff --git a/src/api/guzzle.rs b/src/api/guzzle.rs index 6d1e41b..17d2c0c 100644 --- a/src/api/guzzle.rs +++ b/src/api/guzzle.rs @@ -1,6 +1,7 @@ use crate::api::REQWEST_CLIENT; use crate::Error; +use log::*; use reqwest::StatusCode; use serde::{Deserialize, Serialize}; @@ -19,14 +20,30 @@ pub async fn get_random_teawie() -> Result { .build() .unwrap(); + info!("making request to {}", req.url()); let resp = REQWEST_CLIENT.execute(req).await.unwrap(); + let status = resp.status(); - if let StatusCode::OK = resp.status() { + if let StatusCode::OK = status { match resp.json::().await { Ok(data) => Ok(data.url), - Err(why) => Err(Box::new(why)), + Err(why) => { + if let Some(url) = why.url() { + error!("error parsing json from {}! {}", url, why) + } else { + error!("couldn't even get the url! {}", why); + } + + Err(Box::new(why)) + } } } else { - Err(resp.status().to_string().into()) + error!( + "couldn't fetch random teawie from {}! {}", + resp.url(), + status + ); + + Err(status.to_string().into()) } } -- cgit v1.2.3