1use common_error::ext::{BoxedError, ErrorExt, RetryHint, retry_hint_from_io_error};
16use common_error::status_code::StatusCode;
17use common_macro::stack_trace_debug;
18use snafu::{Location, Snafu};
19
20#[derive(Snafu)]
21#[snafu(visibility(pub))]
22#[stack_trace_debug]
23pub enum Error {
24 #[snafu(display("Invalid config value: {}, {}", value, msg))]
25 InvalidConfig { value: String, msg: String },
26
27 #[snafu(display("Illegal param: {}", msg))]
28 IllegalParam { msg: String },
29
30 #[snafu(display("Internal state error: {}", msg))]
31 InternalState { msg: String },
32
33 #[snafu(display("IO error"))]
34 Io {
35 #[snafu(source)]
36 error: std::io::Error,
37 #[snafu(implicit)]
38 location: Location,
39 },
40
41 #[snafu(display("Authentication source failure"))]
42 AuthBackend {
43 #[snafu(implicit)]
44 location: Location,
45 #[snafu(source)]
46 source: BoxedError,
47 },
48
49 #[snafu(display("User not found, username: {}", username))]
50 UserNotFound { username: String },
51
52 #[snafu(display("Unsupported password type: {}", password_type))]
53 UnsupportedPasswordType { password_type: String },
54
55 #[snafu(display("Username and password does not match, username: {}", username))]
56 UserPasswordMismatch { username: String },
57
58 #[snafu(display(
59 "Access denied for user '{}' to database '{}-{}'",
60 username,
61 catalog,
62 schema
63 ))]
64 AccessDenied {
65 catalog: String,
66 schema: String,
67 username: String,
68 },
69
70 #[snafu(display("Failed to initialize a file watcher"))]
71 FileWatch {
72 #[snafu(source)]
73 source: common_config::error::Error,
74 #[snafu(implicit)]
75 location: Location,
76 },
77
78 #[snafu(display("User is not authorized to perform this action"))]
79 PermissionDenied {
80 #[snafu(implicit)]
81 location: Location,
82 },
83
84 #[snafu(display("Unsupported authentication method: {}", method))]
85 UnsupportedAuthMethod {
86 method: String,
87 #[snafu(implicit)]
88 location: Location,
89 },
90}
91
92impl ErrorExt for Error {
93 fn status_code(&self) -> StatusCode {
94 match self {
95 Error::InvalidConfig { .. } => StatusCode::InvalidArguments,
96 Error::IllegalParam { .. } => StatusCode::InvalidArguments,
97 Error::FileWatch { .. } => StatusCode::InvalidArguments,
98 Error::InternalState { .. } => StatusCode::Unexpected,
99 Error::Io { .. } => StatusCode::StorageUnavailable,
100 Error::AuthBackend { source, .. } => source.status_code(),
101
102 Error::UserNotFound { .. } => StatusCode::UserNotFound,
103 Error::UnsupportedPasswordType { .. } => StatusCode::UnsupportedPasswordType,
104 Error::UserPasswordMismatch { .. } => StatusCode::UserPasswordMismatch,
105 Error::AccessDenied { .. } => StatusCode::AccessDenied,
106 Error::PermissionDenied { .. } => StatusCode::PermissionDenied,
107 Error::UnsupportedAuthMethod { .. } => StatusCode::UserPasswordMismatch,
108 }
109 }
110
111 fn as_any(&self) -> &dyn std::any::Any {
112 self
113 }
114
115 fn retry_hint(&self) -> RetryHint {
116 match self {
117 Error::Io { error, .. } => retry_hint_from_io_error(error),
118 Error::AuthBackend { source, .. } => source.retry_hint(),
119 _ => RetryHint::NonRetryable,
120 }
121 }
122}
123
124pub type Result<T> = std::result::Result<T, Error>;