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