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("Failed to canonicalize path: {}", path))]
63 CanonicalizePath {
64 path: String,
65 #[snafu(source)]
66 error: std::io::Error,
67 #[snafu(implicit)]
68 location: Location,
69 },
70
71 #[snafu(display("Invalid path '{}': expected a file, not a directory", path))]
72 InvalidPath {
73 path: String,
74 #[snafu(implicit)]
75 location: Location,
76 },
77}
78
79impl ErrorExt for Error {
80 fn status_code(&self) -> StatusCode {
81 match self {
82 Error::TomlFormat { .. }
83 | Error::LoadLayeredConfig { .. }
84 | Error::FileWatch { .. }
85 | Error::InvalidPath { .. }
86 | Error::CanonicalizePath { .. } => StatusCode::InvalidArguments,
87 Error::SerdeJson { .. } => StatusCode::Unexpected,
88 }
89 }
90
91 fn as_any(&self) -> &dyn Any {
92 self
93 }
94}