1use std::any::Any;
18
19use arrow_schema::ArrowError;
20use common_error::ext::{BoxedError, ErrorExt};
21use common_error::status_code::StatusCode;
22use common_macro::stack_trace_debug;
23use datafusion_common::DataFusionError;
24use datatypes::data_type::ConcreteDataType;
25use snafu::{Location, Snafu};
26
27#[derive(Snafu)]
31#[snafu(visibility(pub))]
32#[stack_trace_debug]
33pub enum EvalError {
34 #[snafu(display("Fail to cast value of type {from} to given type {to}"))]
35 CastValue {
36 from: ConcreteDataType,
37 to: ConcreteDataType,
38 source: datatypes::Error,
39 #[snafu(implicit)]
40 location: Location,
41 },
42
43 #[snafu(display("{msg}"))]
44 DataType {
45 msg: String,
46 source: datatypes::Error,
47 #[snafu(implicit)]
48 location: Location,
49 },
50
51 #[snafu(display("Invalid argument: {reason}"))]
52 InvalidArgument {
53 reason: String,
54 #[snafu(implicit)]
55 location: Location,
56 },
57
58 #[snafu(display("Internal error: {reason}"))]
59 Internal {
60 reason: String,
61 #[snafu(implicit)]
62 location: Location,
63 },
64
65 #[snafu(display("Arrow error: {error:?}, context: {context}"))]
66 Arrow {
67 #[snafu(source)]
68 error: ArrowError,
69 context: String,
70 #[snafu(implicit)]
71 location: Location,
72 },
73
74 #[snafu(display("DataFusion error: {error:?}, context: {context}"))]
75 Datafusion {
76 #[snafu(source)]
77 error: DataFusionError,
78 context: String,
79 #[snafu(implicit)]
80 location: Location,
81 },
82
83 #[snafu(display("External error"))]
84 External {
85 #[snafu(implicit)]
86 location: Location,
87 source: BoxedError,
88 },
89}
90
91impl ErrorExt for EvalError {
92 fn status_code(&self) -> StatusCode {
93 use EvalError::*;
94 match self {
95 InvalidArgument { .. } => StatusCode::InvalidArguments,
96
97 CastValue { source, .. } | DataType { source, .. } => source.status_code(),
98
99 Internal { .. } | Arrow { .. } | Datafusion { .. } | External { .. } => {
100 StatusCode::Internal
101 }
102 }
103 }
104
105 fn as_any(&self) -> &dyn Any {
106 self
107 }
108}