common_error/
status_code.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::fmt;
16
17use strum::{AsRefStr, EnumIter, EnumString, FromRepr};
18use tonic::Code;
19
20/// Common status code for public API.
21#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumString, AsRefStr, EnumIter, FromRepr)]
22pub enum StatusCode {
23    // ====== Begin of common status code ==============
24    /// Success.
25    Success = 0,
26
27    /// Unknown error.
28    Unknown = 1000,
29    /// Unsupported operation.
30    Unsupported = 1001,
31    /// Unexpected error, maybe there is a BUG.
32    Unexpected = 1002,
33    /// Internal server error.
34    Internal = 1003,
35    /// Invalid arguments.
36    InvalidArguments = 1004,
37    /// The task is cancelled (typically caller-side).
38    Cancelled = 1005,
39    /// Illegal state, can be exposed to users.
40    IllegalState = 1006,
41    /// Caused by some error originated from external system.
42    External = 1007,
43    /// The request is deadline exceeded (typically server-side).
44    DeadlineExceeded = 1008,
45    // ====== End of common status code ================
46
47    // ====== Begin of SQL related status code =========
48    /// SQL Syntax error.
49    InvalidSyntax = 2000,
50    // ====== End of SQL related status code ===========
51
52    // ====== Begin of query related status code =======
53    /// Fail to create a plan for the query.
54    PlanQuery = 3000,
55    /// The query engine fail to execute query.
56    EngineExecuteQuery = 3001,
57    // ====== End of query related status code =========
58
59    // ====== Begin of catalog related status code =====
60    /// Table already exists.
61    TableAlreadyExists = 4000,
62    /// Table not found.
63    TableNotFound = 4001,
64    /// Table column not found.
65    TableColumnNotFound = 4002,
66    /// Table column already exists.
67    TableColumnExists = 4003,
68    /// Database not found.
69    DatabaseNotFound = 4004,
70    /// Region not found.
71    RegionNotFound = 4005,
72    /// Region already exists.
73    RegionAlreadyExists = 4006,
74    /// Region is read-only in current state.
75    RegionReadonly = 4007,
76    /// Region is not in a proper state to handle specific request.
77    RegionNotReady = 4008,
78    /// Region is temporarily in busy state.
79    RegionBusy = 4009,
80    /// Table is temporarily unable to handle the request.
81    TableUnavailable = 4010,
82    /// Database already exists.
83    DatabaseAlreadyExists = 4011,
84    // ====== End of catalog related status code =======
85
86    // ====== Begin of storage related status code =====
87    /// Storage is temporarily unable to handle the request.
88    StorageUnavailable = 5000,
89    /// Request is outdated, e.g., version mismatch.
90    RequestOutdated = 5001,
91    // ====== End of storage related status code =======
92
93    // ====== Begin of server related status code =====
94    /// Runtime resources exhausted, like creating threads failed.
95    RuntimeResourcesExhausted = 6000,
96
97    /// Rate limit exceeded.
98    RateLimited = 6001,
99    // ====== End of server related status code =======
100
101    // ====== Begin of auth related status code =====
102    /// User not exist.
103    UserNotFound = 7000,
104    /// Unsupported password type.
105    UnsupportedPasswordType = 7001,
106    /// Username and password does not match.
107    UserPasswordMismatch = 7002,
108    /// Not found http authorization header.
109    AuthHeaderNotFound = 7003,
110    /// Invalid http authorization header.
111    InvalidAuthHeader = 7004,
112    /// Illegal request to connect catalog-schema.
113    AccessDenied = 7005,
114    /// User is not authorized to perform the operation.
115    PermissionDenied = 7006,
116    // ====== End of auth related status code =====
117
118    // ====== Begin of flow related status code =====
119    FlowAlreadyExists = 8000,
120    FlowNotFound = 8001,
121    // ====== End of flow related status code =====
122
123    // ====== Begin of trigger related status code =====
124    TriggerAlreadyExists = 9000,
125    TriggerNotFound = 9001,
126    // ====== End of trigger related status code =====
127}
128
129impl StatusCode {
130    /// Returns `true` if `code` is success.
131    pub fn is_success(code: u32) -> bool {
132        Self::Success as u32 == code
133    }
134
135    /// Returns `true` if the error with this code is retryable.
136    pub fn is_retryable(&self) -> bool {
137        match self {
138            StatusCode::StorageUnavailable
139            | StatusCode::RuntimeResourcesExhausted
140            | StatusCode::Internal
141            | StatusCode::RegionNotReady
142            | StatusCode::TableUnavailable
143            | StatusCode::RegionBusy => true,
144
145            StatusCode::Success
146            | StatusCode::Unknown
147            | StatusCode::Unsupported
148            | StatusCode::IllegalState
149            | StatusCode::Unexpected
150            | StatusCode::InvalidArguments
151            | StatusCode::Cancelled
152            | StatusCode::DeadlineExceeded
153            | StatusCode::InvalidSyntax
154            | StatusCode::DatabaseAlreadyExists
155            | StatusCode::PlanQuery
156            | StatusCode::EngineExecuteQuery
157            | StatusCode::TableAlreadyExists
158            | StatusCode::TableNotFound
159            | StatusCode::RegionAlreadyExists
160            | StatusCode::RegionNotFound
161            | StatusCode::FlowAlreadyExists
162            | StatusCode::FlowNotFound
163            | StatusCode::TriggerAlreadyExists
164            | StatusCode::TriggerNotFound
165            | StatusCode::RegionReadonly
166            | StatusCode::TableColumnNotFound
167            | StatusCode::TableColumnExists
168            | StatusCode::DatabaseNotFound
169            | StatusCode::RateLimited
170            | StatusCode::UserNotFound
171            | StatusCode::UnsupportedPasswordType
172            | StatusCode::UserPasswordMismatch
173            | StatusCode::AuthHeaderNotFound
174            | StatusCode::InvalidAuthHeader
175            | StatusCode::AccessDenied
176            | StatusCode::PermissionDenied
177            | StatusCode::RequestOutdated
178            | StatusCode::External => false,
179        }
180    }
181
182    /// Returns `true` if we should print an error log for an error with
183    /// this status code.
184    pub fn should_log_error(&self) -> bool {
185        match self {
186            StatusCode::Unknown
187            | StatusCode::Unexpected
188            | StatusCode::Internal
189            | StatusCode::Cancelled
190            | StatusCode::DeadlineExceeded
191            | StatusCode::IllegalState
192            | StatusCode::EngineExecuteQuery
193            | StatusCode::StorageUnavailable
194            | StatusCode::RuntimeResourcesExhausted
195            | StatusCode::External => true,
196
197            StatusCode::Success
198            | StatusCode::Unsupported
199            | StatusCode::InvalidArguments
200            | StatusCode::InvalidSyntax
201            | StatusCode::TableAlreadyExists
202            | StatusCode::TableNotFound
203            | StatusCode::RegionAlreadyExists
204            | StatusCode::RegionNotFound
205            | StatusCode::PlanQuery
206            | StatusCode::FlowAlreadyExists
207            | StatusCode::FlowNotFound
208            | StatusCode::TriggerAlreadyExists
209            | StatusCode::TriggerNotFound
210            | StatusCode::RegionNotReady
211            | StatusCode::RegionBusy
212            | StatusCode::RegionReadonly
213            | StatusCode::TableColumnNotFound
214            | StatusCode::TableColumnExists
215            | StatusCode::DatabaseNotFound
216            | StatusCode::RateLimited
217            | StatusCode::UserNotFound
218            | StatusCode::TableUnavailable
219            | StatusCode::DatabaseAlreadyExists
220            | StatusCode::UnsupportedPasswordType
221            | StatusCode::UserPasswordMismatch
222            | StatusCode::AuthHeaderNotFound
223            | StatusCode::InvalidAuthHeader
224            | StatusCode::AccessDenied
225            | StatusCode::PermissionDenied
226            | StatusCode::RequestOutdated => false,
227        }
228    }
229
230    pub fn from_u32(value: u32) -> Option<Self> {
231        StatusCode::from_repr(value as usize)
232    }
233}
234
235impl fmt::Display for StatusCode {
236    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
237        // The current debug format is suitable to display.
238        write!(f, "{self:?}")
239    }
240}
241
242#[macro_export]
243macro_rules! define_from_tonic_status {
244    ($Error: ty, $Variant: ident) => {
245        impl From<tonic::Status> for $Error {
246            fn from(e: tonic::Status) -> Self {
247                use snafu::location;
248
249                fn metadata_value(e: &tonic::Status, key: &str) -> Option<String> {
250                    e.metadata()
251                        .get(key)
252                        .and_then(|v| String::from_utf8(v.as_bytes().to_vec()).ok())
253                }
254
255                let code = metadata_value(&e, $crate::GREPTIME_DB_HEADER_ERROR_CODE)
256                    .and_then(|s| {
257                        if let Ok(code) = s.parse::<u32>() {
258                            StatusCode::from_u32(code)
259                        } else {
260                            None
261                        }
262                    })
263                    .unwrap_or_else(|| match e.code() {
264                        tonic::Code::Cancelled => StatusCode::Cancelled,
265                        tonic::Code::DeadlineExceeded => StatusCode::DeadlineExceeded,
266                        _ => StatusCode::Internal,
267                    });
268
269                let msg = metadata_value(&e, $crate::GREPTIME_DB_HEADER_ERROR_MSG)
270                    .unwrap_or_else(|| e.message().to_string());
271
272                // TODO(LFC): Make the error variant defined automatically.
273                Self::$Variant {
274                    code,
275                    msg,
276                    tonic_code: e.code(),
277                    location: location!(),
278                }
279            }
280        }
281    };
282}
283
284#[macro_export]
285macro_rules! define_into_tonic_status {
286    ($Error: ty) => {
287        impl From<$Error> for tonic::Status {
288            fn from(err: $Error) -> Self {
289                use tonic::codegen::http::{HeaderMap, HeaderValue};
290                use tonic::metadata::MetadataMap;
291                use $crate::GREPTIME_DB_HEADER_ERROR_CODE;
292
293                let mut headers = HeaderMap::<HeaderValue>::with_capacity(2);
294
295                // If either of the status_code or error msg cannot convert to valid HTTP header value
296                // (which is a very rare case), just ignore. Client will use Tonic status code and message.
297                let status_code = err.status_code();
298                headers.insert(
299                    GREPTIME_DB_HEADER_ERROR_CODE,
300                    HeaderValue::from(status_code as u32),
301                );
302                let root_error = err.output_msg();
303
304                let metadata = MetadataMap::from_headers(headers);
305                tonic::Status::with_metadata(
306                    $crate::status_code::status_to_tonic_code(status_code),
307                    root_error,
308                    metadata,
309                )
310            }
311        }
312    };
313}
314
315/// Returns the tonic [Code] of a [StatusCode].
316pub fn status_to_tonic_code(status_code: StatusCode) -> Code {
317    match status_code {
318        StatusCode::Success => Code::Ok,
319        StatusCode::Unknown | StatusCode::External => Code::Unknown,
320        StatusCode::Unsupported => Code::Unimplemented,
321        StatusCode::Unexpected
322        | StatusCode::IllegalState
323        | StatusCode::Internal
324        | StatusCode::PlanQuery
325        | StatusCode::EngineExecuteQuery => Code::Internal,
326        StatusCode::InvalidArguments | StatusCode::InvalidSyntax | StatusCode::RequestOutdated => {
327            Code::InvalidArgument
328        }
329        StatusCode::Cancelled => Code::Cancelled,
330        StatusCode::DeadlineExceeded => Code::DeadlineExceeded,
331        StatusCode::TableAlreadyExists
332        | StatusCode::TableColumnExists
333        | StatusCode::RegionAlreadyExists
334        | StatusCode::DatabaseAlreadyExists
335        | StatusCode::TriggerAlreadyExists
336        | StatusCode::FlowAlreadyExists => Code::AlreadyExists,
337        StatusCode::TableNotFound
338        | StatusCode::RegionNotFound
339        | StatusCode::TableColumnNotFound
340        | StatusCode::DatabaseNotFound
341        | StatusCode::UserNotFound
342        | StatusCode::TriggerNotFound
343        | StatusCode::FlowNotFound => Code::NotFound,
344        StatusCode::TableUnavailable
345        | StatusCode::StorageUnavailable
346        | StatusCode::RegionNotReady => Code::Unavailable,
347        StatusCode::RuntimeResourcesExhausted
348        | StatusCode::RateLimited
349        | StatusCode::RegionBusy => Code::ResourceExhausted,
350        StatusCode::UnsupportedPasswordType
351        | StatusCode::UserPasswordMismatch
352        | StatusCode::AuthHeaderNotFound
353        | StatusCode::InvalidAuthHeader => Code::Unauthenticated,
354        StatusCode::AccessDenied | StatusCode::PermissionDenied | StatusCode::RegionReadonly => {
355            Code::PermissionDenied
356        }
357    }
358}
359
360#[cfg(test)]
361mod tests {
362    use strum::IntoEnumIterator;
363
364    use super::*;
365
366    fn assert_status_code_display(code: StatusCode, msg: &str) {
367        let code_msg = format!("{code}");
368        assert_eq!(msg, code_msg);
369    }
370
371    #[test]
372    fn test_display_status_code() {
373        assert_status_code_display(StatusCode::Unknown, "Unknown");
374        assert_status_code_display(StatusCode::TableAlreadyExists, "TableAlreadyExists");
375    }
376
377    #[test]
378    fn test_from_u32() {
379        for code in StatusCode::iter() {
380            let num = code as u32;
381            assert_eq!(StatusCode::from_u32(num), Some(code));
382        }
383
384        assert_eq!(StatusCode::from_u32(10000), None);
385    }
386
387    #[test]
388    fn test_is_success() {
389        assert!(StatusCode::is_success(0));
390        assert!(!StatusCode::is_success(1));
391        assert!(!StatusCode::is_success(2));
392        assert!(!StatusCode::is_success(3));
393    }
394}