use std::error::Error as StdError;
use std::time::Duration;
use reqwest::header::RETRY_AFTER;
use reqwest::{Response, StatusCode};
use serde::Deserialize;
use crate::client::API_KEY_ENV;
const UNKNOWN_KIND: &str = "unknown";
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("no API key: call with_api_key or set {API_KEY_ENV}")]
MissingApiKey,
#[error("invalid argument: {0}")]
InvalidArgument(String),
#[error("request failed: {0}")]
Transport(#[source] Box<dyn StdError + Send + Sync>),
#[error("{message} (HTTP {status})")]
#[non_exhaustive]
Api {
status: u16,
kind: String,
message: String,
request_id: Option<String>,
retry_after: Option<Duration>,
next_seq: Option<u64>,
},
#[error("unreadable response: {0}")]
Decode(#[from] serde_json::Error),
}
pub(crate) fn transport(e: reqwest::Error) -> Error {
if e.is_builder() {
return Error::InvalidArgument(e.to_string());
}
Error::Transport(Box::new(e))
}
pub(crate) async fn failure(resp: Response) -> Error {
let status = resp.status();
let retry_after = resp
.headers()
.get(RETRY_AFTER)
.and_then(|v| v.to_str().ok()?.trim().parse().ok())
.map(Duration::from_secs);
api_error(status, retry_after, &resp.bytes().await.unwrap_or_default())
}
fn api_error(status: StatusCode, retry_after: Option<Duration>, body: &[u8]) -> Error {
#[derive(Deserialize)]
struct Envelope {
error: Body,
}
#[derive(Deserialize)]
struct Body {
#[serde(rename = "type")]
kind: String,
message: String,
#[serde(default)]
request_id: Option<String>,
#[serde(default)]
next_seq: Option<u64>,
}
let (kind, message, request_id, next_seq) = match serde_json::from_slice::<Envelope>(body) {
Ok(Envelope { error }) => (error.kind, error.message, error.request_id, error.next_seq),
Err(_) => {
let reason = status.canonical_reason().unwrap_or("request failed");
(UNKNOWN_KIND.to_owned(), reason.to_owned(), None, None)
}
};
Error::Api {
status: status.as_u16(),
kind,
message,
request_id,
retry_after,
next_seq,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_envelope_names_the_kind_and_next_seq() {
let body = br#"{"error":{"type":"conflict","message":"out of order","request_id":"r","next_seq":7}}"#;
let err = api_error(StatusCode::CONFLICT, None, body);
assert!(
matches!(&err, Error::Api { status: 409, kind, next_seq: Some(7), .. } if kind == "conflict"),
"{err:?}"
);
}
#[test]
fn a_body_off_the_envelope_keeps_its_status() {
let err = api_error(
StatusCode::BAD_GATEWAY,
Some(Duration::from_secs(3)),
b"<html>",
);
assert!(
matches!(&err, Error::Api { status: 502, kind, retry_after: Some(d), .. }
if kind == "unknown" && *d == Duration::from_secs(3)),
"{err:?}"
);
}
}