use reqwest::{blocking::Response, StatusCode};
use thiserror::Error;
use crate::config::{HTTP_STATUS_EXPIRED, HTTP_STATUS_UNAUTHORIZED};
use crate::ext::status_code::StatusCodeExt;
pub fn ensure_success(response: &Response) -> Result<(), ResponseError> {
let status = response.status();
if status.is_success() {
return Ok(());
}
if status == HTTP_STATUS_EXPIRED {
return Err(ResponseError::Expired);
}
if status == HTTP_STATUS_UNAUTHORIZED {
return Err(ResponseError::Unauthorized);
}
Err(ResponseError::OtherHttp(status, status.err_text()))
}
#[derive(Error, Debug)]
pub enum ResponseError {
#[error("this file has expired or did never exist")]
Expired,
#[error("unauthorized, are the credentials correct?")]
Unauthorized,
#[error("bad HTTP response: {}", _1)]
OtherHttp(StatusCode, String),
#[error("server responded with undefined error")]
Undefined,
}