Skip to main content

api/
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;
16
17use common_error::ext::ErrorExt;
18use common_error::status_code::StatusCode;
19use common_macro::stack_trace_debug;
20use common_time::timestamp::TimeUnit;
21use datatypes::prelude::ConcreteDataType;
22use snafu::Location;
23use snafu::prelude::*;
24
25pub type Result<T> = std::result::Result<T, Error>;
26
27#[derive(Snafu)]
28#[snafu(visibility(pub))]
29#[stack_trace_debug]
30pub enum Error {
31    #[snafu(display("Unknown proto column datatype: {}", datatype))]
32    UnknownColumnDataType {
33        datatype: i32,
34        #[snafu(implicit)]
35        location: Location,
36        #[snafu(source)]
37        error: prost::UnknownEnumValue,
38    },
39
40    #[snafu(display("Failed to create column datatype from {:?}", from))]
41    IntoColumnDataType {
42        from: ConcreteDataType,
43        #[snafu(implicit)]
44        location: Location,
45    },
46
47    #[snafu(display("Failed to convert column default constraint, column: {}", column))]
48    ConvertColumnDefaultConstraint {
49        column: String,
50        #[snafu(implicit)]
51        location: Location,
52        source: datatypes::error::Error,
53    },
54
55    #[snafu(display("Invalid column default constraint, column: {}", column))]
56    InvalidColumnDefaultConstraint {
57        column: String,
58        #[snafu(implicit)]
59        location: Location,
60        source: datatypes::error::Error,
61    },
62
63    #[snafu(display("Invalid compression options for column {column}"))]
64    InvalidColumnCompression {
65        column: String,
66        #[snafu(implicit)]
67        location: Location,
68        source: datatypes::error::Error,
69    },
70
71    #[snafu(display("Failed to serialize JSON"))]
72    SerializeJson {
73        #[snafu(source)]
74        error: serde_json::Error,
75        #[snafu(implicit)]
76        location: Location,
77    },
78
79    #[snafu(display("Invalid time unit: {time_unit}"))]
80    InvalidTimeUnit {
81        time_unit: i32,
82        #[snafu(implicit)]
83        location: Location,
84    },
85
86    #[snafu(display("Inconsistent time unit: {:?}", units))]
87    InconsistentTimeUnit {
88        units: Vec<TimeUnit>,
89        #[snafu(implicit)]
90        location: Location,
91    },
92}
93
94impl ErrorExt for Error {
95    fn status_code(&self) -> StatusCode {
96        match self {
97            Error::UnknownColumnDataType { .. }
98            | Error::InvalidTimeUnit { .. }
99            | Error::InconsistentTimeUnit { .. } => StatusCode::InvalidArguments,
100            Error::IntoColumnDataType { .. } | Error::SerializeJson { .. } => {
101                StatusCode::Unexpected
102            }
103            Error::ConvertColumnDefaultConstraint { source, .. }
104            | Error::InvalidColumnDefaultConstraint { source, .. }
105            | Error::InvalidColumnCompression { source, .. } => source.status_code(),
106        }
107    }
108
109    fn as_any(&self) -> &dyn Any {
110        self
111    }
112}