diff --git a/src/auth/src/error.rs b/src/auth/src/error.rs index 23c5f0d66c..281c45234d 100644 --- a/src/auth/src/error.rs +++ b/src/auth/src/error.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use common_error::ext::ErrorExt; +use common_error::ext::{BoxedError, ErrorExt}; use common_error::status_code::StatusCode; use common_macro::stack_trace_debug; use snafu::{Location, Snafu}; @@ -38,6 +38,14 @@ pub enum Error { location: Location, }, + #[snafu(display("Authentication source failure"))] + AuthBackend { + #[snafu(implicit)] + location: Location, + #[snafu(source)] + source: BoxedError, + }, + #[snafu(display("User not found, username: {}", username))] UserNotFound { username: String }, @@ -81,6 +89,7 @@ impl ErrorExt for Error { Error::FileWatch { .. } => StatusCode::InvalidArguments, Error::InternalState { .. } => StatusCode::Unexpected, Error::Io { .. } => StatusCode::StorageUnavailable, + Error::AuthBackend { .. } => StatusCode::Internal, Error::UserNotFound { .. } => StatusCode::UserNotFound, Error::UnsupportedPasswordType { .. } => StatusCode::UnsupportedPasswordType, diff --git a/src/auth/src/tests.rs b/src/auth/src/tests.rs index 8e3cd17e7a..ef5bf9a6b5 100644 --- a/src/auth/src/tests.rs +++ b/src/auth/src/tests.rs @@ -13,9 +13,11 @@ // limitations under the License. use common_base::secrets::ExposeSecret; +use common_error::ext::BoxedError; +use snafu::{OptionExt, ResultExt}; use crate::error::{ - AccessDeniedSnafu, Result, UnsupportedPasswordTypeSnafu, UserNotFoundSnafu, + AccessDeniedSnafu, AuthBackendSnafu, Result, UnsupportedPasswordTypeSnafu, UserNotFoundSnafu, UserPasswordMismatchSnafu, }; use crate::user_info::DefaultUserInfo; @@ -49,6 +51,19 @@ impl MockUserProvider { info.schema.clone_into(&mut self.schema); info.username.clone_into(&mut self.username); } + + // this is a deliberate function to ref AuthBackendSnafu + // so that it won't get deleted in the future + pub fn ref_auth_backend_snafu(&self) -> Result<()> { + let none_option = None; + + none_option + .context(UserNotFoundSnafu { + username: "no_user".to_string(), + }) + .map_err(BoxedError::new) + .context(AuthBackendSnafu) + } } #[async_trait::async_trait]