Skip to main content

webex_message_handler/
errors.rs

1//! Error types for webex-message-handler.
2
3use thiserror::Error;
4
5/// Base error type for all webex-message-handler errors.
6#[derive(Error, Debug)]
7pub enum WebexError {
8    /// Token is invalid, expired, or unauthorized.
9    #[error("AUTH_ERROR: {0}")]
10    Auth(String),
11
12    /// WDM device registration/refresh/unregister failed.
13    #[error("DEVICE_REGISTRATION_ERROR: {message}")]
14    DeviceRegistration {
15        message: String,
16        status_code: Option<u16>,
17    },
18
19    /// WebSocket connection to Mercury failed.
20    #[error("MERCURY_CONNECTION_ERROR: {message}")]
21    MercuryConnection {
22        message: String,
23        close_code: Option<u16>,
24    },
25
26    /// KMS key exchange or key retrieval failed.
27    #[error("KMS_ERROR: {0}")]
28    Kms(String),
29
30    /// Message decryption failed.
31    #[error("DECRYPTION_ERROR: {0}")]
32    Decryption(String),
33
34    /// Generic internal error.
35    #[error("{0}")]
36    Internal(String),
37}
38
39impl WebexError {
40    pub fn auth(msg: impl Into<String>) -> Self {
41        Self::Auth(msg.into())
42    }
43
44    pub fn device_registration(msg: impl Into<String>, status_code: Option<u16>) -> Self {
45        Self::DeviceRegistration {
46            message: msg.into(),
47            status_code,
48        }
49    }
50
51    pub fn mercury_connection(msg: impl Into<String>, close_code: Option<u16>) -> Self {
52        Self::MercuryConnection {
53            message: msg.into(),
54            close_code,
55        }
56    }
57
58    pub fn kms(msg: impl Into<String>) -> Self {
59        Self::Kms(msg.into())
60    }
61
62    pub fn decryption(msg: impl Into<String>) -> Self {
63        Self::Decryption(msg.into())
64    }
65
66    /// Returns the error code string.
67    pub fn code(&self) -> &'static str {
68        match self {
69            Self::Auth(_) => "AUTH_ERROR",
70            Self::DeviceRegistration { .. } => "DEVICE_REGISTRATION_ERROR",
71            Self::MercuryConnection { .. } => "MERCURY_CONNECTION_ERROR",
72            Self::Kms(_) => "KMS_ERROR",
73            Self::Decryption(_) => "DECRYPTION_ERROR",
74            Self::Internal(_) => "INTERNAL_ERROR",
75        }
76    }
77}
78
79pub type Result<T> = std::result::Result<T, WebexError>;