use crate::Result;
pub fn check_esri_error(response_text: &str, operation: &str) -> Result<()> {
if !response_text.contains("\"error\"") {
return Ok(());
}
tracing::error!(operation = %operation, response = %response_text, "ESRI returned error in response body");
#[derive(serde::Deserialize)]
struct ErrorResponse1 {
error: ErrorDetail1,
}
#[derive(serde::Deserialize)]
struct ErrorDetail1 {
code: i32,
message: String,
}
if let Ok(err) = serde_json::from_str::<ErrorResponse1>(response_text) {
return Err(crate::Error::from(crate::ErrorKind::Api {
code: err.error.code,
message: err.error.message,
}));
}
#[derive(serde::Deserialize)]
struct ErrorResponse2 {
success: bool,
error: ErrorDetail2,
}
#[derive(serde::Deserialize)]
struct ErrorDetail2 {
message: String,
#[serde(default)]
code: Option<i32>,
}
if let Ok(err) = serde_json::from_str::<ErrorResponse2>(response_text) {
if !err.success {
return Err(crate::Error::from(crate::ErrorKind::Api {
code: err.error.code.unwrap_or(0),
message: err.error.message,
}));
}
}
Err(crate::Error::from(crate::ErrorKind::Api {
code: -1,
message: format!("{} returned unexpected error: {}", operation, response_text),
}))
}