Skip to main content

pysochrone/
error.rs

1#[derive(Debug)]
2pub enum OsmGraphError {
3    Network(reqwest::Error),
4    XmlParse(quick_xml::DeError),
5    EmptyGraph,
6    NodeNotFound,
7    LockPoisoned,
8    GeocodingFailed(String),
9    InvalidInput(String),
10    Io(std::io::Error),
11    PbfError(String),
12}
13
14impl std::fmt::Display for OsmGraphError {
15    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16        match self {
17            OsmGraphError::Network(e)         => write!(f, "Network error: {}", e),
18            OsmGraphError::XmlParse(e)        => write!(f, "XML parse error: {}", e),
19            OsmGraphError::EmptyGraph         => write!(f, "Graph is empty"),
20            OsmGraphError::NodeNotFound       => write!(f, "No node found near the given coordinates"),
21            OsmGraphError::LockPoisoned       => write!(f, "Internal cache lock was poisoned"),
22            OsmGraphError::GeocodingFailed(p) => write!(f, "Could not geocode '{}'", p),
23            OsmGraphError::InvalidInput(msg)  => write!(f, "Invalid input: {}", msg),
24            OsmGraphError::Io(e)              => write!(f, "IO error: {}", e),
25            OsmGraphError::PbfError(msg)      => write!(f, "PBF error: {}", msg),
26        }
27    }
28}
29
30impl std::error::Error for OsmGraphError {}
31
32impl From<reqwest::Error> for OsmGraphError {
33    fn from(e: reqwest::Error) -> Self { OsmGraphError::Network(e) }
34}
35
36impl From<quick_xml::DeError> for OsmGraphError {
37    fn from(e: quick_xml::DeError) -> Self { OsmGraphError::XmlParse(e) }
38}
39
40impl From<std::io::Error> for OsmGraphError {
41    fn from(e: std::io::Error) -> Self { OsmGraphError::Io(e) }
42}
43
44// Only compile the pyo3 conversion when building the Python extension.
45#[cfg(feature = "extension-module")]
46impl From<OsmGraphError> for pyo3::PyErr {
47    fn from(e: OsmGraphError) -> Self {
48        pyo3::exceptions::PyRuntimeError::new_err(e.to_string())
49    }
50}