geojson/
errors.rs

1//! Module for all GeoJSON-related errors
2use crate::Feature;
3use serde_json::value::Value;
4use thiserror::Error;
5
6/// Errors which can occur when encoding, decoding, and converting GeoJSON
7#[derive(Error, Debug)]
8pub enum Error {
9    #[error("Encountered non-array value for a 'bbox' object: `{0}`")]
10    BboxExpectedArray(Value),
11    #[error("Encountered non-numeric value within 'bbox' array")]
12    BboxExpectedNumericValues(Value),
13    #[error("Encountered a non-object type for GeoJSON: `{0}`")]
14    GeoJsonExpectedObject(Value),
15    /// This was previously `GeoJsonUnknownType`, but has been split for clarity
16    #[error("Expected a Feature, FeatureCollection, or Geometry, but got an empty type")]
17    EmptyType,
18    #[error("invalid writer state: {0}")]
19    InvalidWriterState(&'static str),
20    #[error("IO Error: {0}")]
21    Io(std::io::Error),
22    /// This was previously `GeoJsonUnknownType`, but has been split for clarity
23    #[error("Expected a Feature mapping, but got a `{0}`")]
24    NotAFeature(String),
25    #[error("Expected type: `{expected_type}`, but found `{found_type}`")]
26    InvalidGeometryConversion {
27        expected_type: &'static str,
28        found_type: &'static str,
29    },
30    #[error(
31        "Attempted to a convert a feature without a geometry into a geo_types::Geometry: `{0}`"
32    )]
33    FeatureHasNoGeometry(Feature),
34    #[error("Encountered an unknown 'geometry' object type: `{0}`")]
35    GeometryUnknownType(String),
36    #[error("Error while deserializing JSON: {0}")]
37    MalformedJson(serde_json::Error),
38    #[error("Encountered neither object type nor null type for 'properties' object: `{0}`")]
39    PropertiesExpectedObjectOrNull(Value),
40    #[error("Encountered neither object type nor null type for 'geometry' field on 'feature' object: `{0}`")]
41    FeatureInvalidGeometryValue(Value),
42    #[error(
43        "Encountered neither number type nor string type for 'id' field on 'feature' object: `{0}`"
44    )]
45    FeatureInvalidIdentifierType(Value),
46    #[error("Expected GeoJSON type `{expected}`, found `{actual}`")]
47    ExpectedType { expected: String, actual: String },
48    #[error("Expected a String value, but got a `{0}`")]
49    ExpectedStringValue(Value),
50    #[error("Expected a GeoJSON property for `{0}`, but got None")]
51    ExpectedProperty(String),
52    #[error("Expected a floating-point value, but got None")]
53    ExpectedF64Value,
54    #[error("Expected an Array value, but got `{0}`")]
55    ExpectedArrayValue(String),
56    #[error("Expected an owned Object, but got `{0}`")]
57    ExpectedObjectValue(Value),
58    #[error("A position must contain two or more elements, but got `{0}`")]
59    PositionTooShort(usize),
60}
61
62pub type Result<T> = std::result::Result<T, Error>;
63
64impl From<serde_json::Error> for Error {
65    fn from(error: serde_json::Error) -> Self {
66        Self::MalformedJson(error)
67    }
68}
69
70impl From<std::io::Error> for Error {
71    fn from(error: std::io::Error) -> Self {
72        Self::Io(error)
73    }
74}