#[cfg(feature = "python")]
use pyo3::prelude::PyErr;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum GeoError {
#[error("API request failed: {0}")]
RequestError(#[from] reqwest::Error),
#[error("JSON parsing failed: {0}")]
ParseError(#[from] serde_json::Error),
#[error("Configuration error: {0}")]
ConfigError(String),
#[error("Google API error: {status} - {message}")]
ApiError { status: String, message: String },
#[error("No results found for the given query")]
ZeroResults,
#[error("Unknown error: {0}")]
Unknown(String),
}
impl GeoError {
pub fn json_rpc_code(&self) -> i32 {
match self {
GeoError::RequestError(_) => -32001, GeoError::ParseError(_) => -32700, GeoError::ConfigError(_) => -32002, GeoError::ApiError { .. } => -32003, GeoError::ZeroResults => -32602, GeoError::Unknown(_) => -32603, }
}
}
#[cfg(feature = "python")]
impl From<GeoError> for PyErr {
fn from(err: GeoError) -> PyErr {
match err {
GeoError::ConfigError(msg) => pyo3::exceptions::PyValueError::new_err(msg),
GeoError::ZeroResults => pyo3::exceptions::PyValueError::new_err("No results found"),
GeoError::ApiError { status, message } => {
pyo3::exceptions::PyRuntimeError::new_err(format!("{}: {}", status, message))
}
_ => pyo3::exceptions::PyRuntimeError::new_err(err.to_string()),
}
}
}