1use std::error::Error as StdError;
8use thiserror::Error;
9
10#[derive(Debug, Error)]
15pub enum GateError {
16 #[error("Invalid geometry: {message}")]
18 InvalidGeometry { message: String },
19
20 #[error("Missing parameter '{parameter}' in context: {context}")]
22 MissingParameter { parameter: String, context: String },
23
24 #[error("Invalid coordinate '{coordinate}': value {value} is not finite or out of range")]
26 InvalidCoordinate { coordinate: String, value: f32 },
27
28 #[error("Filtering error: {message}")]
30 FilteringError { message: String },
31
32 #[error("Hierarchy error: {message}")]
34 HierarchyError { message: String },
35
36 #[error("Serialization error: {0}")]
38 SerializationError(#[from] serde_json::Error),
39
40 #[error("Index error: {message}")]
42 IndexError { message: String },
43
44 #[error("{message}")]
46 Other {
47 message: String,
48 #[source]
49 source: Option<Box<dyn StdError + Send + Sync>>,
50 },
51}
52
53impl GateError {
54 pub fn invalid_geometry(message: impl Into<String>) -> Self {
56 Self::InvalidGeometry {
57 message: message.into(),
58 }
59 }
60
61 pub fn missing_parameter(parameter: impl Into<String>, context: impl Into<String>) -> Self {
63 Self::MissingParameter {
64 parameter: parameter.into(),
65 context: context.into(),
66 }
67 }
68
69 pub fn invalid_coordinate(coordinate: impl Into<String>, value: f32) -> Self {
71 Self::InvalidCoordinate {
72 coordinate: coordinate.into(),
73 value,
74 }
75 }
76
77 pub fn filtering_error(message: impl Into<String>) -> Self {
79 Self::FilteringError {
80 message: message.into(),
81 }
82 }
83
84 pub fn hierarchy_error(message: impl Into<String>) -> Self {
86 Self::HierarchyError {
87 message: message.into(),
88 }
89 }
90
91 pub fn index_error(message: impl Into<String>) -> Self {
93 Self::IndexError {
94 message: message.into(),
95 }
96 }
97
98 pub fn with_context(self, context: impl Into<String>) -> Self {
100 match self {
101 Self::InvalidGeometry { message } => Self::InvalidGeometry {
102 message: format!("{}: {}", context.into(), message),
103 },
104 Self::MissingParameter {
105 parameter,
106 context: ctx,
107 } => Self::MissingParameter {
108 parameter,
109 context: format!("{}: {}", context.into(), ctx),
110 },
111 Self::InvalidCoordinate { coordinate, value } => {
112 Self::InvalidCoordinate { coordinate, value }
113 }
114 Self::FilteringError { message } => Self::FilteringError {
115 message: format!("{}: {}", context.into(), message),
116 },
117 Self::HierarchyError { message } => Self::HierarchyError {
118 message: format!("{}: {}", context.into(), message),
119 },
120 Self::SerializationError(e) => Self::Other {
121 message: format!("{}: {}", context.into(), e),
122 source: Some(Box::new(e)),
123 },
124 Self::IndexError { message } => Self::IndexError {
125 message: format!("{}: {}", context.into(), message),
126 },
127 Self::Other { message, source } => Self::Other {
128 message: format!("{}: {}", context.into(), message),
129 source,
130 },
131 }
132 }
133}
134
135impl From<anyhow::Error> for GateError {
137 fn from(err: anyhow::Error) -> Self {
138 Self::Other {
139 message: err.to_string(),
140 source: None, }
142 }
143}
144
145pub type Result<T> = std::result::Result<T, GateError>;