1use std::any::Any;
16
17use common_error::define_from_tonic_status;
18use common_error::ext::{BoxedError, ErrorExt};
19use common_error::status_code::StatusCode;
20use common_macro::stack_trace_debug;
21use snafu::{location, Location, Snafu};
22use tonic::metadata::errors::InvalidMetadataValue;
23use tonic::Code;
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 #[snafu(implicit)]
134 location: Location,
135 },
136}
137
138pub type Result<T> = std::result::Result<T, Error>;
139
140impl ErrorExt for Error {
141 fn status_code(&self) -> StatusCode {
142 match self {
143 Error::IllegalFlightMessages { .. }
144 | Error::MissingField { .. }
145 | Error::IllegalDatabaseResponse { .. } => StatusCode::Internal,
146
147 Error::Server { code, .. } | Error::Tonic { code, .. } => *code,
148 Error::FlightGet { source, .. }
149 | Error::RegionServer { source, .. }
150 | Error::FlowServer { source, .. } => source.status_code(),
151 Error::CreateChannel { source, .. }
152 | Error::ConvertFlightData { source, .. }
153 | Error::CreateTlsChannel { source, .. } => source.status_code(),
154 Error::IllegalGrpcClientState { .. } => StatusCode::Unexpected,
155 Error::InvalidTonicMetadataValue { .. } => StatusCode::InvalidArguments,
156 Error::ConvertSchema { source, .. } => source.status_code(),
157 }
158 }
159
160 fn as_any(&self) -> &dyn Any {
161 self
162 }
163}
164
165define_from_tonic_status!(Error, Tonic);
166
167impl Error {
168 pub fn should_retry(&self) -> bool {
169 matches!(
171 self,
172 Self::RegionServer {
173 code: Code::Cancelled,
174 ..
175 } | Self::RegionServer {
176 code: Code::DeadlineExceeded,
177 ..
178 } | Self::RegionServer {
179 code: Code::Unavailable,
180 ..
181 }
182 )
183 }
184}