common_config/
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 config::ConfigError;
21use snafu::{Location, Snafu};
22
23pub type Result<T> = std::result::Result<T, Error>;
24
25#[derive(Snafu)]
26#[snafu(visibility(pub))]
27#[stack_trace_debug]
28pub enum Error {
29    #[snafu(display("Failed to load layered config"))]
30    LoadLayeredConfig {
31        #[snafu(source)]
32        error: ConfigError,
33        #[snafu(implicit)]
34        location: Location,
35    },
36
37    #[snafu(display("Failed to serde json"))]
38    SerdeJson {
39        #[snafu(source)]
40        error: serde_json::error::Error,
41        #[snafu(implicit)]
42        location: Location,
43    },
44
45    #[snafu(display("Failed to serialize options to TOML"))]
46    TomlFormat {
47        #[snafu(source)]
48        error: toml::ser::Error,
49        #[snafu(implicit)]
50        location: Location,
51    },
52}
53
54impl ErrorExt for Error {
55    fn status_code(&self) -> StatusCode {
56        match self {
57            Error::TomlFormat { .. } | Error::LoadLayeredConfig { .. } => {
58                StatusCode::InvalidArguments
59            }
60            Error::SerdeJson { .. } => StatusCode::Unexpected,
61        }
62    }
63
64    fn as_any(&self) -> &dyn Any {
65        self
66    }
67}