common_event_recorder/
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 api::v1::ColumnSchema;
16use common_error::ext::{BoxedError, ErrorExt};
17use common_error::status_code::StatusCode;
18use common_macro::stack_trace_debug;
19use snafu::{Location, Snafu};
20
21#[derive(Snafu)]
22#[snafu(visibility(pub))]
23#[stack_trace_debug]
24pub enum Error {
25    #[snafu(display("Mismatched schema, expected: {:?}, actual: {:?}", expected, actual))]
26    MismatchedSchema {
27        #[snafu(implicit)]
28        location: Location,
29        expected: Vec<ColumnSchema>,
30        actual: Vec<ColumnSchema>,
31    },
32
33    #[snafu(display("Failed to serialize event"))]
34    SerializeEvent {
35        #[snafu(source)]
36        error: serde_json::error::Error,
37        #[snafu(implicit)]
38        location: Location,
39    },
40
41    #[snafu(display("Failed to insert events"))]
42    InsertEvents {
43        // BoxedError is utilized here to prevent introducing a circular dependency that would arise from directly referencing `client::error::Error`.
44        source: BoxedError,
45        #[snafu(implicit)]
46        location: Location,
47    },
48
49    #[snafu(display("Keyvalue backend error"))]
50    KvBackend {
51        // BoxedError is utilized here to prevent introducing a circular dependency that would arise from directly referencing `common_meta::error::Error`.
52        source: BoxedError,
53        #[snafu(implicit)]
54        location: Location,
55    },
56}
57
58pub type Result<T> = std::result::Result<T, Error>;
59
60impl ErrorExt for Error {
61    fn status_code(&self) -> StatusCode {
62        match self {
63            Error::MismatchedSchema { .. } | Error::SerializeEvent { .. } => {
64                StatusCode::InvalidArguments
65            }
66            Error::InsertEvents { .. } | Error::KvBackend { .. } => StatusCode::Internal,
67        }
68    }
69
70    fn as_any(&self) -> &dyn std::any::Any {
71        self
72    }
73}