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::prelude::*;
23use snafu::Location;
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("Failed to serialize JSON"))]
64    SerializeJson {
65        #[snafu(source)]
66        error: serde_json::Error,
67        #[snafu(implicit)]
68        location: Location,
69    },
70
71    #[snafu(display("Invalid time unit: {time_unit}"))]
72    InvalidTimeUnit {
73        time_unit: i32,
74        #[snafu(implicit)]
75        location: Location,
76    },
77
78    #[snafu(display("Inconsistent time unit: {:?}", units))]
79    InconsistentTimeUnit {
80        units: Vec<TimeUnit>,
81        #[snafu(implicit)]
82        location: Location,
83    },
84}
85
86impl ErrorExt for Error {
87    fn status_code(&self) -> StatusCode {
88        match self {
89            Error::UnknownColumnDataType { .. }
90            | Error::InvalidTimeUnit { .. }
91            | Error::InconsistentTimeUnit { .. } => StatusCode::InvalidArguments,
92            Error::IntoColumnDataType { .. } | Error::SerializeJson { .. } => {
93                StatusCode::Unexpected
94            }
95            Error::ConvertColumnDefaultConstraint { source, .. }
96            | Error::InvalidColumnDefaultConstraint { source, .. } => source.status_code(),
97        }
98    }
99
100    fn as_any(&self) -> &dyn Any {
101        self
102    }
103}