maps-service/validation/api/src/lib.rs

59 lines
1.4 KiB
Rust
Raw Normal View History

mod context;
2024-12-18 22:20:01 +00:00
pub use context::Cookie;
#[cfg(feature="internal")]
pub mod internal;
//lazy reexport
pub type ReqwestError=reqwest::Error;
#[derive(Debug)]
2024-12-07 03:36:10 +00:00
pub enum Error{
ParseError(url::ParseError),
Reqwest(reqwest::Error),
Response(ResponseError),
2024-12-14 08:15:05 +00:00
JSON(serde_json::Error),
}
2024-12-07 03:36:10 +00:00
impl std::fmt::Display for Error{
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
write!(f,"{self:?}")
}
}
2024-12-07 03:36:10 +00:00
impl std::error::Error for Error{}
#[allow(dead_code)]
#[derive(Debug)]
pub struct StatusCodeWithUrlAndBody{
pub status_code:reqwest::StatusCode,
pub url:url::Url,
pub body:String,
}
#[derive(Debug)]
pub enum ResponseError{
Reqwest(reqwest::Error),
StatusCodeWithUrlAndBody(StatusCodeWithUrlAndBody),
}
impl std::fmt::Display for ResponseError{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f,"{self:?}")
}
}
impl std::error::Error for ResponseError{}
// lazy function to draw out meaningful info from http response on failure
pub async fn response_ok(response:reqwest::Response)->Result<reqwest::Response,ResponseError>{
let status_code=response.status();
if status_code.is_success(){
Ok(response)
}else{
let url=response.url().to_owned();
let bytes=response.bytes().await.map_err(ResponseError::Reqwest)?;
let body=String::from_utf8_lossy(&bytes).to_string();
Err(ResponseError::StatusCodeWithUrlAndBody(StatusCodeWithUrlAndBody{
status_code,
url,
body,
}))
}
}