Skip to main content

mito_codec/
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::{Location, Snafu};
22
23/// Error definitions for mito encoding.
24#[derive(Snafu)]
25#[snafu(visibility(pub))]
26#[stack_trace_debug]
27pub enum Error {
28    #[snafu(display("Row value mismatches field data type"))]
29    FieldTypeMismatch {
30        // Box the source to reduce the size of the error.
31        #[snafu(source(from(datatypes::error::Error, Box::new)))]
32        source: Box<datatypes::error::Error>,
33        #[snafu(implicit)]
34        location: Location,
35    },
36
37    #[snafu(display("Failed to serialize field"))]
38    SerializeField {
39        #[snafu(source)]
40        error: memcomparable::Error,
41        #[snafu(implicit)]
42        location: Location,
43    },
44
45    #[snafu(display(
46        "Data type: {} does not support serialization/deserialization",
47        data_type,
48    ))]
49    NotSupportedField {
50        data_type: ConcreteDataType,
51        #[snafu(implicit)]
52        location: Location,
53    },
54
55    #[snafu(display("Failed to deserialize field"))]
56    DeserializeField {
57        #[snafu(source)]
58        error: memcomparable::Error,
59        #[snafu(implicit)]
60        location: Location,
61    },
62
63    #[snafu(display("Operation not supported: {}", err_msg))]
64    UnsupportedOperation {
65        err_msg: String,
66        #[snafu(implicit)]
67        location: Location,
68    },
69
70    #[snafu(display("Invalid sparse primary key: {}", reason))]
71    InvalidSparsePrimaryKey {
72        reason: String,
73        #[snafu(implicit)]
74        location: Location,
75    },
76
77    #[snafu(display("Invalid dense primary key: {}", reason))]
78    InvalidDensePrimaryKey {
79        reason: String,
80        #[snafu(implicit)]
81        location: Location,
82    },
83
84    #[snafu(display("Invalid label decode description"))]
85    InvalidLabelDecodeDescription {
86        // Box the source to reduce the size of the error.
87        #[snafu(source(from(datatypes::error::Error, Box::new)))]
88        source: Box<datatypes::error::Error>,
89        #[snafu(implicit)]
90        location: Location,
91    },
92
93    #[snafu(display("Encode null value"))]
94    IndexEncodeNull {
95        #[snafu(implicit)]
96        location: Location,
97    },
98
99    #[snafu(display("Failed to evaluate filter"))]
100    EvaluateFilter {
101        #[snafu(source(from(common_recordbatch::error::Error, Box::new)))]
102        source: Box<common_recordbatch::error::Error>,
103        #[snafu(implicit)]
104        location: Location,
105    },
106}
107
108pub type Result<T, E = Error> = std::result::Result<T, E>;
109
110impl ErrorExt for Error {
111    fn status_code(&self) -> StatusCode {
112        use Error::*;
113
114        match self {
115            FieldTypeMismatch { source, .. } => source.status_code(),
116            SerializeField { .. } | DeserializeField { .. } | IndexEncodeNull { .. } => {
117                StatusCode::InvalidArguments
118            }
119            NotSupportedField { .. } | UnsupportedOperation { .. } => StatusCode::Unsupported,
120            InvalidLabelDecodeDescription { source, .. } => source.status_code(),
121            InvalidSparsePrimaryKey { .. } | InvalidDensePrimaryKey { .. } => {
122                StatusCode::InvalidArguments
123            }
124            EvaluateFilter { source, .. } => source.status_code(),
125        }
126    }
127
128    fn as_any(&self) -> &dyn Any {
129        self
130    }
131}