Skip to main content

index/bloom_filter/
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::sync::Arc;
17
18use common_error::ext::{BoxedError, ErrorExt};
19use common_error::status_code::StatusCode;
20use common_macro::stack_trace_debug;
21use snafu::{Location, Snafu};
22
23#[derive(Snafu)]
24#[snafu(visibility(pub))]
25#[stack_trace_debug]
26pub enum Error {
27    #[snafu(display("IO error"))]
28    Io {
29        #[snafu(source)]
30        error: std::io::Error,
31        #[snafu(implicit)]
32        location: Location,
33    },
34
35    #[snafu(display("Failed to decode protobuf"))]
36    DecodeProto {
37        #[snafu(source)]
38        error: prost::DecodeError,
39        #[snafu(implicit)]
40        location: Location,
41    },
42
43    #[snafu(display("Intermediate error"))]
44    Intermediate {
45        source: crate::error::Error,
46        #[snafu(implicit)]
47        location: Location,
48    },
49
50    #[snafu(display("File size too small for bloom filter"))]
51    FileSizeTooSmall {
52        size: u64,
53        #[snafu(implicit)]
54        location: Location,
55    },
56
57    #[snafu(display("Unexpected bloom filter meta size"))]
58    UnexpectedMetaSize {
59        max_meta_size: u64,
60        actual_meta_size: u64,
61        #[snafu(implicit)]
62        location: Location,
63    },
64
65    #[snafu(display("Invalid intermediate magic"))]
66    InvalidIntermediateMagic {
67        invalid: Vec<u8>,
68        #[snafu(implicit)]
69        location: Location,
70    },
71
72    #[snafu(display("Shared index metadata read failed"))]
73    SharedRead {
74        source: Arc<Error>,
75        #[snafu(implicit)]
76        location: Location,
77    },
78
79    #[snafu(display("External error"))]
80    External {
81        source: BoxedError,
82        #[snafu(implicit)]
83        location: Location,
84    },
85}
86
87impl ErrorExt for Error {
88    fn status_code(&self) -> StatusCode {
89        use Error::*;
90
91        match self {
92            Io { .. }
93            | FileSizeTooSmall { .. }
94            | UnexpectedMetaSize { .. }
95            | DecodeProto { .. }
96            | InvalidIntermediateMagic { .. } => StatusCode::Unexpected,
97
98            Intermediate { source, .. } => source.status_code(),
99            External { source, .. } => source.status_code(),
100            SharedRead { source, .. } => source.status_code(),
101        }
102    }
103
104    fn as_any(&self) -> &dyn Any {
105        self
106    }
107}
108
109pub type Result<T> = std::result::Result<T, Error>;
110
111#[cfg(test)]
112mod tests {
113    use snafu::IntoError;
114
115    use super::*;
116
117    #[test]
118    fn shared_metadata_error_preserves_status() {
119        let source = FileSizeTooSmallSnafu { size: 0_u64 }.build();
120        let status = source.status_code();
121        let retry = source.retry_hint();
122        let error = SharedReadSnafu.into_error(Arc::new(source));
123        assert_eq!(error.status_code(), status);
124        assert_eq!(error.retry_hint(), retry);
125    }
126}