1use 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}