Skip to main content

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
50pub type Result<T> = std::result::Result<T, Error>;
51
52impl ErrorExt for Error {
53    fn status_code(&self) -> StatusCode {
54        match self {
55            Error::MismatchedSchema { .. } | Error::SerializeEvent { .. } => {
56                StatusCode::InvalidArguments
57            }
58            Error::InsertEvents { .. } => StatusCode::Internal,
59        }
60    }
61
62    fn as_any(&self) -> &dyn std::any::Any {
63        self
64    }
65}