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