Skip to main content

common_grpc/
error.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::any::Any;
16use std::io;
17
18use common_error::ext::{ErrorExt, RetryHint, retry_hint_from_io_error};
19use common_error::status_code::StatusCode;
20use common_macro::stack_trace_debug;
21use datatypes::arrow::error::ArrowError;
22use snafu::{Location, Snafu};
23
24pub type Result<T> = std::result::Result<T, Error>;
25
26#[derive(Snafu)]
27#[snafu(visibility(pub))]
28#[stack_trace_debug]
29pub enum Error {
30    #[snafu(display("Invalid config file path"))]
31    InvalidConfigFilePath {
32        #[snafu(source)]
33        error: io::Error,
34        #[snafu(implicit)]
35        location: Location,
36    },
37
38    #[snafu(display("Failed to watch config file"))]
39    FileWatch {
40        #[snafu(source)]
41        source: common_config::error::Error,
42        #[snafu(implicit)]
43        location: Location,
44    },
45
46    #[snafu(display("Failed to create gRPC channel from '{addr}'"))]
47    CreateChannel {
48        addr: String,
49        #[snafu(source)]
50        error: tonic::transport::Error,
51        #[snafu(implicit)]
52        location: Location,
53    },
54
55    #[snafu(display("Failed to convert Arrow type: {}", from))]
56    Conversion {
57        from: String,
58        #[snafu(implicit)]
59        location: Location,
60    },
61
62    #[snafu(display("Failed to decode FlightData"))]
63    DecodeFlightData {
64        #[snafu(source)]
65        error: api::DecodeError,
66        #[snafu(implicit)]
67        location: Location,
68    },
69
70    #[snafu(display("Invalid FlightData, reason: {}", reason))]
71    InvalidFlightData {
72        reason: String,
73        #[snafu(implicit)]
74        location: Location,
75    },
76
77    #[snafu(display("Not supported: {}", feat))]
78    NotSupported { feat: String },
79
80    #[snafu(display("Failed to serde Json"))]
81    SerdeJson {
82        #[snafu(source)]
83        error: serde_json::error::Error,
84        #[snafu(implicit)]
85        location: Location,
86    },
87
88    #[snafu(display("Failed arrow operation"))]
89    Arrow {
90        #[snafu(implicit)]
91        location: Location,
92        #[snafu(source)]
93        error: ArrowError,
94    },
95}
96
97impl ErrorExt for Error {
98    fn status_code(&self) -> StatusCode {
99        match self {
100            Error::InvalidConfigFilePath { .. }
101            | Error::FileWatch { .. }
102            | Error::InvalidFlightData { .. }
103            | Error::NotSupported { .. } => StatusCode::InvalidArguments,
104
105            Error::CreateChannel { .. }
106            | Error::Conversion { .. }
107            | Error::DecodeFlightData { .. }
108            | Error::SerdeJson { .. } => StatusCode::Internal,
109
110            Error::Arrow { .. } => StatusCode::Internal,
111        }
112    }
113
114    fn as_any(&self) -> &dyn Any {
115        self
116    }
117
118    fn retry_hint(&self) -> RetryHint {
119        match self {
120            Error::CreateChannel { .. } => RetryHint::Retryable,
121            Error::Arrow { error, .. } => retry_hint_from_arrow_error(error),
122            Error::FileWatch { source, .. } => source.retry_hint(),
123            _ => RetryHint::NonRetryable,
124        }
125    }
126}
127
128fn retry_hint_from_arrow_error(error: &ArrowError) -> RetryHint {
129    match error {
130        ArrowError::IoError(_, error) => retry_hint_from_io_error(error),
131        _ => RetryHint::NonRetryable,
132    }
133}