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 #[snafu(display("Failed to watch file: {}", path))]
54 FileWatch {
55 path: String,
56 #[snafu(source)]
57 error: notify::Error,
58 #[snafu(implicit)]
59 location: Location,
60 },
61
62 #[snafu(display("Invalid path '{}': expected a file, not a directory", path))]
63 InvalidPath {
64 path: String,
65 #[snafu(implicit)]
66 location: Location,
67 },
68}
69
70impl ErrorExt for Error {
71 fn status_code(&self) -> StatusCode {
72 match self {
73 Error::TomlFormat { .. }
74 | Error::LoadLayeredConfig { .. }
75 | Error::FileWatch { .. }
76 | Error::InvalidPath { .. } => StatusCode::InvalidArguments,
77 Error::SerdeJson { .. } => StatusCode::Unexpected,
78 }
79 }
80
81 fn as_any(&self) -> &dyn Any {
82 self
83 }
84}