1use std::any::Any;
16
17use common_error::define_from_tonic_status;
18use common_error::ext::{BoxedError, ErrorExt, RetryHint};
19use common_error::status_code::StatusCode;
20use common_macro::stack_trace_debug;
21use snafu::{Location, Snafu};
22use tonic::Code;
23use tonic::metadata::errors::InvalidMetadataValue;
24
25#[derive(Snafu)]
26#[snafu(visibility(pub))]
27#[stack_trace_debug]
28pub enum Error {
29 #[snafu(display("Illegal Flight messages, reason: {}", reason))]
30 IllegalFlightMessages {
31 reason: String,
32 #[snafu(implicit)]
33 location: Location,
34 },
35
36 #[snafu(display("Failed to do Flight get, code: {}", tonic_code))]
37 FlightGet {
38 addr: String,
39 tonic_code: Code,
40 source: BoxedError,
41 },
42
43 #[snafu(display("Failed to convert FlightData"))]
44 ConvertFlightData {
45 #[snafu(implicit)]
46 location: Location,
47 source: common_grpc::Error,
48 },
49
50 #[snafu(display("Illegal GRPC client state: {}", err_msg))]
51 IllegalGrpcClientState {
52 err_msg: String,
53 #[snafu(implicit)]
54 location: Location,
55 },
56
57 #[snafu(display("Missing required field in protobuf, field: {}", field))]
58 MissingField {
59 field: String,
60 #[snafu(implicit)]
61 location: Location,
62 },
63
64 #[snafu(display("Failed to create gRPC channel, peer address: {}", addr))]
65 CreateChannel {
66 addr: String,
67 #[snafu(implicit)]
68 location: Location,
69 source: common_grpc::error::Error,
70 },
71
72 #[snafu(display("Failed to create Tls channel manager"))]
73 CreateTlsChannel {
74 #[snafu(implicit)]
75 location: Location,
76 source: common_grpc::error::Error,
77 },
78
79 #[snafu(display("Failed to request RegionServer {}, code: {}", addr, code))]
80 RegionServer {
81 addr: String,
82 code: Code,
83 source: BoxedError,
84 #[snafu(implicit)]
85 location: Location,
86 },
87
88 #[snafu(display("Failed to request FlowServer {}, code: {}", addr, code))]
89 FlowServer {
90 addr: String,
91 code: Code,
92 source: BoxedError,
93 #[snafu(implicit)]
94 location: Location,
95 },
96
97 #[snafu(display("{}", msg))]
99 Server {
100 code: StatusCode,
101 msg: String,
102 #[snafu(implicit)]
103 location: Location,
104 },
105
106 #[snafu(display("Illegal Database response: {err_msg}"))]
107 IllegalDatabaseResponse {
108 err_msg: String,
109 #[snafu(implicit)]
110 location: Location,
111 },
112
113 #[snafu(display("Invalid Tonic metadata value"))]
114 InvalidTonicMetadataValue {
115 #[snafu(source)]
116 error: InvalidMetadataValue,
117 #[snafu(implicit)]
118 location: Location,
119 },
120
121 #[snafu(display("Failed to convert Schema"))]
122 ConvertSchema {
123 #[snafu(implicit)]
124 location: Location,
125 source: datatypes::error::Error,
126 },
127
128 #[snafu(display("{}", msg))]
129 Tonic {
130 code: StatusCode,
131 msg: String,
132 tonic_code: Code,
133 retry_hint: RetryHint,
134 #[snafu(implicit)]
135 location: Location,
136 },
137
138 #[snafu(display("External error"))]
139 External {
140 #[snafu(implicit)]
141 location: Location,
142 source: BoxedError,
143 },
144}
145
146pub type Result<T> = std::result::Result<T, Error>;
147
148impl ErrorExt for Error {
149 fn status_code(&self) -> StatusCode {
150 match self {
151 Error::IllegalFlightMessages { .. }
152 | Error::MissingField { .. }
153 | Error::IllegalDatabaseResponse { .. } => StatusCode::Internal,
154
155 Error::Server { code, .. } | Error::Tonic { code, .. } => *code,
156 Error::FlightGet { source, .. }
157 | Error::RegionServer { source, .. }
158 | Error::FlowServer { source, .. } => source.status_code(),
159 Error::CreateChannel { source, .. }
160 | Error::ConvertFlightData { source, .. }
161 | Error::CreateTlsChannel { source, .. } => source.status_code(),
162 Error::IllegalGrpcClientState { .. } => StatusCode::Unexpected,
163 Error::InvalidTonicMetadataValue { .. } => StatusCode::InvalidArguments,
164 Error::ConvertSchema { source, .. } => source.status_code(),
165 Error::External { source, .. } => source.status_code(),
166 }
167 }
168
169 fn as_any(&self) -> &dyn Any {
170 self
171 }
172
173 fn retry_hint(&self) -> RetryHint {
174 match self {
175 Error::Tonic { retry_hint, .. } => *retry_hint,
176 Error::FlightGet { source, .. }
177 | Error::RegionServer { source, .. }
178 | Error::FlowServer { source, .. }
179 | Error::External { source, .. } => source.retry_hint(),
180 Error::ConvertFlightData { source, .. }
181 | Error::CreateChannel { source, .. }
182 | Error::CreateTlsChannel { source, .. } => source.retry_hint(),
183 Error::ConvertSchema { source, .. } => source.retry_hint(),
184 _ => RetryHint::NonRetryable,
185 }
186 }
187}
188
189define_from_tonic_status!(Error, Tonic);
190
191impl Error {
192 pub fn tonic_code(&self) -> Option<Code> {
194 match self {
195 Self::FlightGet { tonic_code, .. }
196 | Self::RegionServer {
197 code: tonic_code, ..
198 }
199 | Self::FlowServer {
200 code: tonic_code, ..
201 }
202 | Self::Tonic { tonic_code, .. } => Some(*tonic_code),
203 _ => None,
204 }
205 }
206
207 pub fn is_connection_error(&self) -> bool {
209 matches!(self.tonic_code(), Some(Code::Unavailable))
210 }
211
212 pub fn should_retry(&self) -> bool {
213 self.retry_hint().is_retryable()
214 || self.is_connection_error()
215 || matches!(
216 self.tonic_code(),
217 Some(Code::Cancelled) | Some(Code::DeadlineExceeded)
218 )
219 }
220}