Skip to main content

basemapper_core/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Error)]
4pub enum BasemapError {
5    #[error("invalid bbox: {0}")]
6    InvalidBbox(String),
7    #[error("invalid dimensions {0}×{1}")]
8    InvalidDimensions(u32, u32),
9    #[error("style fetch failed for {url}: HTTP {status}")]
10    StyleFetchFailed { url: String, status: u16 },
11    #[error("style parse error: {0}")]
12    StyleParseError(String),
13    #[error("tile fetch failed for {url}: HTTP {status}")]
14    TileFetchFailed { url: String, status: u16 },
15    /// A tile URL answered with a web page (a sign-in or error page), as
16    /// some services do for a wrong or missing key. The message leaves the
17    /// URL out: it may carry the key.
18    #[error("the tile server sent a web page instead of a tile: check the API key (or quad word) and that it gives access to this basemap")]
19    TileIsWebPage { url: String },
20    #[error("tile decode error: {0}")]
21    TileDecodeError(String),
22    #[error("render error: {0}")]
23    RenderError(String),
24    #[error("max tiles exceeded: requested {requested}, limit {limit}")]
25    MaxTilesExceeded { requested: u32, limit: u32 },
26    #[error("invalid layer filter: {0}")]
27    InvalidLayerFilter(String),
28    #[error("invalid input: {0}")]
29    InvalidInput(String),
30}
31
32impl BasemapError {
33    /// Return a stable, coarse category string for mapping to typed Python exceptions.
34    ///
35    /// Variants:
36    /// - `"validation"` → `ValidationError` (invalid input before any I/O)
37    /// - `"style"`      → `StyleError` (style fetch or parse failure)
38    /// - `"network"`    → `NetworkError` (tile fetch failure)
39    /// - `"render"`     → `BasemapError` (everything else — GPU / decode)
40    pub fn kind(&self) -> &'static str {
41        match self {
42            BasemapError::InvalidBbox(_)
43            | BasemapError::InvalidDimensions(_, _)
44            | BasemapError::InvalidLayerFilter(_)
45            | BasemapError::InvalidInput(_)
46            | BasemapError::MaxTilesExceeded { .. } => "validation",
47            BasemapError::StyleFetchFailed { .. } | BasemapError::StyleParseError(_) => "style",
48            BasemapError::TileFetchFailed { .. } | BasemapError::TileIsWebPage { .. } => "network",
49            BasemapError::RenderError(_) | BasemapError::TileDecodeError(_) => "render",
50        }
51    }
52}