use crate::error_code::StandardErrorCode;
use serde_yaml::Value;
pub fn classify_to_standard_code(error_class: &str) -> StandardErrorCode {
StandardErrorCode::from_error_class(error_class)
}
pub fn classify_error_from_response(
http_status: u16,
response_body: Option<&Value>,
) -> StandardErrorCode {
if let Some(body) = response_body {
if let Some(code) = extract_provider_error_code(body) {
if let Some(std_code) = StandardErrorCode::from_provider_code(&code) {
return std_code;
}
}
}
StandardErrorCode::from_http_status(http_status)
}
fn extract_provider_error_code(body: &Value) -> Option<String> {
let mapping = body.as_mapping()?;
if let Some(err) = mapping.get("error") {
if let Some(err_map) = err.as_mapping() {
if let Some(code) = err_map.get("code").and_then(|v| v.as_str()) {
return Some(code.to_string());
}
if let Some(ty) = err_map.get("type").and_then(|v| v.as_str()) {
return Some(ty.to_string());
}
}
}
None
}
pub(crate) fn is_fallbackable_error_class(error_class: &str) -> bool {
classify_to_standard_code(error_class).fallbackable()
}