aster_forge_external_auth/
error.rs1pub type Result<T> = std::result::Result<T, ExternalAuthError>;
9
10#[derive(Debug, thiserror::Error)]
12pub enum ExternalAuthError {
13 #[error("{0}")]
15 Validation(String),
16 #[error("{0}")]
18 Config(String),
19 #[error("{0}")]
22 InvalidCredentials(String),
23 #[error("{0}")]
25 State(String),
26 #[error("{0}")]
28 Internal(String),
29}
30
31impl ExternalAuthError {
32 pub fn validation_error(message: impl Into<String>) -> Self {
34 Self::Validation(message.into())
35 }
36
37 pub fn config_error(message: impl Into<String>) -> Self {
39 Self::Config(message.into())
40 }
41
42 pub fn auth_invalid_credentials(message: impl Into<String>) -> Self {
44 Self::InvalidCredentials(message.into())
45 }
46
47 pub fn state_error(message: impl Into<String>) -> Self {
49 Self::State(message.into())
50 }
51
52 pub fn database_operation(message: impl Into<String>) -> Self {
55 Self::State(message.into())
56 }
57
58 pub fn internal_error(message: impl Into<String>) -> Self {
60 Self::Internal(message.into())
61 }
62}
63
64impl From<aster_forge_validation::ValidationError> for ExternalAuthError {
65 fn from(error: aster_forge_validation::ValidationError) -> Self {
66 Self::validation_error(error.to_string())
67 }
68}
69
70pub trait MapExternalAuthErr<T> {
72 fn map_external_auth_err_ctx(
74 self,
75 context: impl Into<String>,
76 error_fn: fn(String) -> ExternalAuthError,
77 ) -> Result<T>;
78}
79
80impl<T, E> MapExternalAuthErr<T> for std::result::Result<T, E>
81where
82 E: std::fmt::Display,
83{
84 fn map_external_auth_err_ctx(
85 self,
86 context: impl Into<String>,
87 error_fn: fn(String) -> ExternalAuthError,
88 ) -> Result<T> {
89 let context = context.into();
90 self.map_err(|error| error_fn(format!("{context}: {error}")))
91 }
92}