Skip to main content

rustauth_plugins/captcha/
error.rs

1//! CAPTCHA errors.
2
3use rustauth_core::error_codes::ErrorCode;
4
5#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
6pub enum CaptchaConfigError {
7    #[error("missing CAPTCHA secret key")]
8    MissingSecretKey,
9    #[error("failed to serialize CAPTCHA options: {0}")]
10    SerializeOptions(String),
11}
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum CaptchaErrorCode {
15    VerificationFailed,
16    MissingResponse,
17    UnknownError,
18}
19
20impl CaptchaErrorCode {
21    pub fn as_str(self) -> &'static str {
22        match self {
23            Self::VerificationFailed => "VERIFICATION_FAILED",
24            Self::MissingResponse => "MISSING_RESPONSE",
25            Self::UnknownError => "CAPTCHA_UNKNOWN_ERROR",
26        }
27    }
28
29    pub fn message(self) -> &'static str {
30        match self {
31            Self::VerificationFailed => "Captcha verification failed",
32            Self::MissingResponse => "Missing CAPTCHA response",
33            Self::UnknownError => "Something went wrong",
34        }
35    }
36}
37
38impl ErrorCode for CaptchaErrorCode {
39    fn as_str(&self) -> &str {
40        (*self).as_str()
41    }
42
43    fn message(&self) -> &str {
44        (*self).message()
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51    use rustauth_core::error_codes::ErrorCode;
52
53    fn assert_error_code(code: impl ErrorCode, expected_code: &str, expected_message: &str) {
54        assert_eq!(code.as_str(), expected_code);
55        assert_eq!(code.message(), expected_message);
56    }
57
58    #[test]
59    fn captcha_error_code_implements_error_code_trait() {
60        assert_error_code(
61            CaptchaErrorCode::VerificationFailed,
62            "VERIFICATION_FAILED",
63            "Captcha verification failed",
64        );
65    }
66}