Skip to main content

shapefile_wasm/
error.rs

1use thiserror::Error;
2
3/// Every failure mode the writer can produce, with enough context to point a
4/// caller at the offending feature.
5#[derive(Debug, Error)]
6pub enum ShapefileError {
7    #[error("could not read the input as GeoJSON: {0}")]
8    Input(String),
9
10    #[error("unsupported GeoJSON geometry type `{0}`")]
11    UnsupportedGeometry(String),
12
13    #[error(
14        "a shapefile holds a single geometry type, but the input mixes {first} and {other} \
15         (feature {index}); split the input or pass an explicit `shapeType`"
16    )]
17    MixedGeometry {
18        first: String,
19        other: String,
20        index: usize,
21    },
22
23    #[error("feature {index}: {message}")]
24    Feature { index: usize, message: String },
25
26    #[error("GeometryCollection is not representable in a shapefile (feature {index})")]
27    GeometryCollection { index: usize },
28
29    #[error("the input contains no writable features")]
30    Empty,
31
32    #[error("field name `{0}` could not be reduced to a valid dBase field name")]
33    FieldName(String),
34
35    #[error("error writing geometry: {0}")]
36    Shape(#[from] shapefile::Error),
37
38    #[error("error writing attributes: {0}")]
39    Dbf(#[from] dbase::Error),
40}
41
42pub type Result<T> = std::result::Result<T, ShapefileError>;
43
44impl From<ShapefileError> for wasm_bindgen::JsValue {
45    fn from(err: ShapefileError) -> Self {
46        wasm_bindgen::JsError::new(&err.to_string()).into()
47    }
48}