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 datatypes::prelude::ConcreteDataType;
21use snafu::prelude::*;
22use snafu::Location;
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("Unknown proto column datatype: {}", datatype))]
31    UnknownColumnDataType {
32        datatype: i32,
33        #[snafu(implicit)]
34        location: Location,
35        #[snafu(source)]
36        error: prost::UnknownEnumValue,
37    },
38
39    #[snafu(display("Failed to create column datatype from {:?}", from))]
40    IntoColumnDataType {
41        from: ConcreteDataType,
42        #[snafu(implicit)]
43        location: Location,
44    },
45
46    #[snafu(display("Failed to convert column default constraint, column: {}", column))]
47    ConvertColumnDefaultConstraint {
48        column: String,
49        #[snafu(implicit)]
50        location: Location,
51        source: datatypes::error::Error,
52    },
53
54    #[snafu(display("Invalid column default constraint, column: {}", column))]
55    InvalidColumnDefaultConstraint {
56        column: String,
57        #[snafu(implicit)]
58        location: Location,
59        source: datatypes::error::Error,
60    },
61
62    #[snafu(display("Failed to serialize JSON"))]
63    SerializeJson {
64        #[snafu(source)]
65        error: serde_json::Error,
66        #[snafu(implicit)]
67        location: Location,
68    },
69}
70
71impl ErrorExt for Error {
72    fn status_code(&self) -> StatusCode {
73        match self {
74            Error::UnknownColumnDataType { .. } => StatusCode::InvalidArguments,
75            Error::IntoColumnDataType { .. } | Error::SerializeJson { .. } => {
76                StatusCode::Unexpected
77            }
78            Error::ConvertColumnDefaultConstraint { source, .. }
79            | Error::InvalidColumnDefaultConstraint { source, .. } => source.status_code(),
80        }
81    }
82
83    fn as_any(&self) -> &dyn Any {
84        self
85    }
86}