1use std::any::Any;
16use std::fmt::Debug;
17
18use common_error::ext::{BoxedError, ErrorExt};
19use common_error::status_code::StatusCode;
20use common_macro::stack_trace_debug;
21use common_query::error::datafusion_status_code;
22use datafusion::error::DataFusionError;
23use snafu::{Location, Snafu};
24
25#[derive(Snafu)]
26#[snafu(visibility(pub))]
27#[stack_trace_debug]
28pub enum Error {
29 #[snafu(display("Failed to list catalogs"))]
30 ListCatalogs {
31 #[snafu(implicit)]
32 location: Location,
33 source: BoxedError,
34 },
35
36 #[snafu(display("Failed to list {}'s schemas", catalog))]
37 ListSchemas {
38 #[snafu(implicit)]
39 location: Location,
40 catalog: String,
41 source: BoxedError,
42 },
43
44 #[snafu(display("Failed to list {}.{}'s tables", catalog, schema))]
45 ListTables {
46 #[snafu(implicit)]
47 location: Location,
48 catalog: String,
49 schema: String,
50 source: BoxedError,
51 },
52
53 #[snafu(display("Failed to list nodes in cluster"))]
54 ListNodes {
55 #[snafu(implicit)]
56 location: Location,
57 source: BoxedError,
58 },
59
60 #[snafu(display("Failed to region stats in cluster"))]
61 ListRegionStats {
62 #[snafu(implicit)]
63 location: Location,
64 source: BoxedError,
65 },
66
67 #[snafu(display("Failed to list flow stats"))]
68 ListFlowStats {
69 #[snafu(implicit)]
70 location: Location,
71 source: BoxedError,
72 },
73
74 #[snafu(display("Failed to list flows in catalog {catalog}"))]
75 ListFlows {
76 #[snafu(implicit)]
77 location: Location,
78 catalog: String,
79 source: BoxedError,
80 },
81
82 #[snafu(display("Flow info not found: {flow_name} in catalog {catalog_name}"))]
83 FlowInfoNotFound {
84 flow_name: String,
85 catalog_name: String,
86 #[snafu(implicit)]
87 location: Location,
88 },
89
90 #[snafu(display("Can't convert value to json, input={input}"))]
91 Json {
92 input: String,
93 #[snafu(source)]
94 error: serde_json::error::Error,
95 #[snafu(implicit)]
96 location: Location,
97 },
98
99 #[snafu(display("Failed to get information extension client"))]
100 GetInformationExtension {
101 #[snafu(implicit)]
102 location: Location,
103 },
104
105 #[snafu(display("Failed to list procedures"))]
106 ListProcedures {
107 #[snafu(implicit)]
108 location: Location,
109 source: BoxedError,
110 },
111
112 #[snafu(display("Procedure id not found"))]
113 ProcedureIdNotFound {
114 #[snafu(implicit)]
115 location: Location,
116 },
117
118 #[snafu(display("convert proto data error"))]
119 ConvertProtoData {
120 #[snafu(implicit)]
121 location: Location,
122 source: BoxedError,
123 },
124
125 #[snafu(display("Cannot find catalog by name: {}", catalog_name))]
126 CatalogNotFound {
127 catalog_name: String,
128 #[snafu(implicit)]
129 location: Location,
130 },
131
132 #[snafu(display("Cannot find schema {} in catalog {}", schema, catalog))]
133 SchemaNotFound {
134 catalog: String,
135 schema: String,
136 #[snafu(implicit)]
137 location: Location,
138 },
139
140 #[snafu(display("Table `{}` already exists", table))]
141 TableExists {
142 table: String,
143 #[snafu(implicit)]
144 location: Location,
145 },
146
147 #[snafu(display("Table not found: {}", table))]
148 TableNotExist {
149 table: String,
150 #[snafu(implicit)]
151 location: Location,
152 },
153
154 #[snafu(display("View info not found: {}", name))]
155 ViewInfoNotFound {
156 name: String,
157 #[snafu(implicit)]
158 location: Location,
159 },
160
161 #[snafu(display(
162 "View plan columns changed from: {} to: {}",
163 origin_names,
164 actual_names
165 ))]
166 ViewPlanColumnsChanged {
167 origin_names: String,
168 actual_names: String,
169 #[snafu(implicit)]
170 location: Location,
171 },
172
173 #[snafu(display("Partition manager not found, it's not expected."))]
174 PartitionManagerNotFound {
175 #[snafu(implicit)]
176 location: Location,
177 },
178
179 #[snafu(display("Failed to find table partitions"))]
180 FindPartitions { source: partition::error::Error },
181
182 #[snafu(display("Failed to find region routes"))]
183 FindRegionRoutes { source: partition::error::Error },
184
185 #[snafu(display("Failed to create recordbatch"))]
186 CreateRecordBatch {
187 #[snafu(implicit)]
188 location: Location,
189 source: common_recordbatch::error::Error,
190 },
191
192 #[snafu(display("Internal error"))]
193 Internal {
194 #[snafu(implicit)]
195 location: Location,
196 source: BoxedError,
197 },
198
199 #[snafu(display("Failed to upgrade weak catalog manager reference"))]
200 UpgradeWeakCatalogManagerRef {
201 #[snafu(implicit)]
202 location: Location,
203 },
204
205 #[snafu(display("Failed to decode logical plan for view: {}", name))]
206 DecodePlan {
207 name: String,
208 #[snafu(implicit)]
209 location: Location,
210 source: common_query::error::Error,
211 },
212
213 #[snafu(display("Illegal access to catalog: {} and schema: {}", catalog, schema))]
214 QueryAccessDenied { catalog: String, schema: String },
215
216 #[snafu(display("DataFusion error"))]
217 Datafusion {
218 #[snafu(source)]
219 error: DataFusionError,
220 #[snafu(implicit)]
221 location: Location,
222 },
223
224 #[snafu(display("Failed to project view columns"))]
225 ProjectViewColumns {
226 #[snafu(source)]
227 error: DataFusionError,
228 #[snafu(implicit)]
229 location: Location,
230 },
231
232 #[snafu(display("Table metadata manager error"))]
233 TableMetadataManager {
234 source: common_meta::error::Error,
235 #[snafu(implicit)]
236 location: Location,
237 },
238
239 #[snafu(display("Failed to get table cache"))]
240 GetTableCache {
241 source: common_meta::error::Error,
242 #[snafu(implicit)]
243 location: Location,
244 },
245
246 #[snafu(display("Failed to get view info from cache"))]
247 GetViewCache {
248 source: common_meta::error::Error,
249 #[snafu(implicit)]
250 location: Location,
251 },
252
253 #[snafu(display("Cache not found: {name}"))]
254 CacheNotFound {
255 name: String,
256 #[snafu(implicit)]
257 location: Location,
258 },
259
260 #[snafu(display("Failed to cast the catalog manager"))]
261 CastManager {
262 #[snafu(implicit)]
263 location: Location,
264 },
265
266 #[snafu(display("Failed to invoke frontend services"))]
267 InvokeFrontend {
268 source: common_frontend::error::Error,
269 #[snafu(implicit)]
270 location: Location,
271 },
272
273 #[snafu(display("Meta client is not provided"))]
274 MetaClientMissing {
275 #[snafu(implicit)]
276 location: Location,
277 },
278
279 #[snafu(display("Failed to find frontend node: {}", addr))]
280 FrontendNotFound {
281 addr: String,
282 #[snafu(implicit)]
283 location: Location,
284 },
285
286 #[snafu(display("Failed to handle query, addr: {}", addr))]
287 HandleQuery {
288 addr: String,
289 source: common_meta::error::Error,
290 #[snafu(implicit)]
291 location: Location,
292 },
293
294 #[snafu(display("Failed to project schema"))]
295 ProjectSchema {
296 source: datatypes::error::Error,
297 #[snafu(implicit)]
298 location: Location,
299 },
300}
301
302impl Error {
303 pub fn should_fail(&self) -> bool {
304 use Error::*;
305
306 matches!(
307 self,
308 GetViewCache { .. }
309 | ViewInfoNotFound { .. }
310 | DecodePlan { .. }
311 | ViewPlanColumnsChanged { .. }
312 | ProjectViewColumns { .. }
313 )
314 }
315}
316
317pub type Result<T> = std::result::Result<T, Error>;
318
319impl ErrorExt for Error {
320 fn status_code(&self) -> StatusCode {
321 match self {
322 Error::SchemaNotFound { .. }
323 | Error::CatalogNotFound { .. }
324 | Error::FindPartitions { .. }
325 | Error::FindRegionRoutes { .. }
326 | Error::CacheNotFound { .. }
327 | Error::CastManager { .. }
328 | Error::Json { .. }
329 | Error::GetInformationExtension { .. }
330 | Error::PartitionManagerNotFound { .. }
331 | Error::ProcedureIdNotFound { .. } => StatusCode::Unexpected,
332
333 Error::ViewPlanColumnsChanged { .. } => StatusCode::InvalidArguments,
334
335 Error::ViewInfoNotFound { .. } => StatusCode::TableNotFound,
336
337 Error::FlowInfoNotFound { .. } => StatusCode::FlowNotFound,
338
339 Error::UpgradeWeakCatalogManagerRef { .. } => StatusCode::Internal,
340
341 Error::CreateRecordBatch { source, .. } => source.status_code(),
342 Error::TableExists { .. } => StatusCode::TableAlreadyExists,
343 Error::TableNotExist { .. } => StatusCode::TableNotFound,
344 Error::ListCatalogs { source, .. }
345 | Error::ListNodes { source, .. }
346 | Error::ListSchemas { source, .. }
347 | Error::ListTables { source, .. }
348 | Error::ListFlows { source, .. }
349 | Error::ListFlowStats { source, .. }
350 | Error::ListProcedures { source, .. }
351 | Error::ListRegionStats { source, .. }
352 | Error::ConvertProtoData { source, .. } => source.status_code(),
353
354 Error::DecodePlan { source, .. } => source.status_code(),
355
356 Error::Internal { source, .. } => source.status_code(),
357
358 Error::QueryAccessDenied { .. } => StatusCode::AccessDenied,
359 Error::Datafusion { error, .. } => datafusion_status_code::<Self>(error, None),
360 Error::ProjectViewColumns { .. } => StatusCode::EngineExecuteQuery,
361 Error::TableMetadataManager { source, .. } => source.status_code(),
362 Error::GetViewCache { source, .. } | Error::GetTableCache { source, .. } => {
363 source.status_code()
364 }
365 Error::InvokeFrontend { source, .. } => source.status_code(),
366 Error::FrontendNotFound { .. } | Error::MetaClientMissing { .. } => {
367 StatusCode::Unexpected
368 }
369 Error::HandleQuery { source, .. } => source.status_code(),
370 Error::ProjectSchema { source, .. } => source.status_code(),
371 }
372 }
373
374 fn as_any(&self) -> &dyn Any {
375 self
376 }
377}
378
379impl From<Error> for DataFusionError {
380 fn from(e: Error) -> Self {
381 DataFusionError::External(Box::new(e))
382 }
383}
384
385#[cfg(test)]
386mod tests {
387 use snafu::GenerateImplicitData;
388
389 use super::*;
390
391 #[test]
392 pub fn test_errors_to_datafusion_error() {
393 let e: DataFusionError = Error::TableExists {
394 table: "test_table".to_string(),
395 location: Location::generate(),
396 }
397 .into();
398 match e {
399 DataFusionError::External(_) => {}
400 _ => {
401 panic!("catalog error should be converted to DataFusionError::Internal")
402 }
403 }
404 }
405}