common_error/lib.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
15#![feature(error_iter)]
16
17pub mod ext;
18pub mod mock;
19pub mod status_code;
20
21use http::{HeaderMap, HeaderValue};
22pub use snafu;
23
24use crate::status_code::StatusCode;
25
26// HACK - these headers are here for shared in gRPC services. For common HTTP headers,
27// please define in `src/servers/src/http/header.rs`.
28pub const GREPTIME_DB_HEADER_ERROR_CODE: &str = "x-greptime-err-code";
29pub const GREPTIME_DB_HEADER_ERROR_MSG: &str = "x-greptime-err-msg";
30pub const GREPTIME_DB_HEADER_ERROR_RETRY_HINT: &str = "x-greptime-err-retry-hint";
31
32/// Create a http header map from error code and message.
33/// using `GREPTIME_DB_HEADER_ERROR_CODE` and `GREPTIME_DB_HEADER_ERROR_MSG` as keys.
34pub fn from_err_code_msg_to_header(code: u32, msg: &str) -> HeaderMap {
35 let mut header = HeaderMap::new();
36
37 let msg = HeaderValue::from_str(msg).unwrap_or_else(|_| {
38 HeaderValue::from_bytes(
39 &msg.as_bytes()
40 .iter()
41 .flat_map(|b| std::ascii::escape_default(*b))
42 .collect::<Vec<u8>>(),
43 )
44 .expect("Already escaped string should be valid ascii")
45 });
46
47 header.insert(GREPTIME_DB_HEADER_ERROR_CODE, code.into());
48 header.insert(GREPTIME_DB_HEADER_ERROR_MSG, msg);
49 header
50}
51
52/// Extract [StatusCode] and error message from [HeaderMap], if any.
53///
54/// Note that if the [StatusCode] is illegal, for example, a random number that is not pre-defined
55/// as a [StatusCode], the result is still `None`.
56pub fn from_header_to_err_code_msg(headers: &HeaderMap) -> Option<(StatusCode, &str)> {
57 let code = headers
58 .get(GREPTIME_DB_HEADER_ERROR_CODE)
59 .and_then(|value| {
60 value
61 .to_str()
62 .ok()
63 .and_then(|x| x.parse::<u32>().ok())
64 .and_then(StatusCode::from_u32)
65 });
66 let msg = headers
67 .get(GREPTIME_DB_HEADER_ERROR_MSG)
68 .and_then(|x| x.to_str().ok());
69 match (code, msg) {
70 (Some(code), Some(msg)) => Some((code, msg)),
71 _ => None,
72 }
73}
74
75/// Returns the external root cause of the source error (exclude the current error).
76pub fn root_source(err: &dyn std::error::Error) -> Option<&dyn std::error::Error> {
77 // There are some divergence about the behavior of the `sources()` API
78 // in https://github.com/rust-lang/rust/issues/58520
79 // So this function iterates the sources manually.
80 let mut root = err.source();
81 while let Some(r) = root {
82 if let Some(s) = r.source() {
83 root = Some(s);
84 } else {
85 break;
86 }
87 }
88 root
89}