Skip to main content

sedona_geometry/
error.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17use std::{io, num};
18
19use thiserror::Error;
20
21#[derive(Error, Debug)]
22pub enum SedonaGeometryError {
23    #[error("{0}")]
24    Invalid(String),
25    #[error("{0}")]
26    IO(io::Error),
27    #[error("{0}")]
28    External(Box<dyn std::error::Error + Send + Sync>),
29    #[error("Unknown geometry error")]
30    Unknown,
31}
32
33impl From<io::Error> for SedonaGeometryError {
34    fn from(value: io::Error) -> Self {
35        SedonaGeometryError::IO(value)
36    }
37}
38
39impl From<num::TryFromIntError> for SedonaGeometryError {
40    fn from(value: num::TryFromIntError) -> Self {
41        SedonaGeometryError::External(Box::new(value))
42    }
43}
44
45#[cfg(test)]
46mod test {
47    use super::*;
48
49    #[test]
50    fn errors() {
51        let invalid = SedonaGeometryError::Invalid("foofy".to_string());
52        assert_eq!(invalid.to_string(), "foofy");
53
54        let some_err = Box::new(std::io::Error::new(std::io::ErrorKind::Deadlock, "foofy"));
55        let external = SedonaGeometryError::External(some_err);
56        assert_eq!(external.to_string(), "foofy");
57
58        let unknown = SedonaGeometryError::Unknown;
59        assert_eq!(unknown.to_string(), "Unknown geometry error");
60    }
61}