1use thiserror::Error;
2use std::io;
3use url::ParseError;
4use reqwest;
5use serde_json;
6
7#[derive(Error, Debug)]
10pub enum PusherError {
11 #[error("HTTP error: {0}")]
12 HttpError(#[from] reqwest::Error),
13
14 #[error("WebSocket error: {0}")]
15 WebSocketError(String),
16
17 #[error("JSON error: {0}")]
18 JsonError(#[from] serde_json::Error),
19
20 #[error("I/O error: {0}")]
21 IoError(#[from] io::Error),
22
23 #[error("URL parse error: {0}")]
24 UrlParseError(#[from] ParseError),
25
26 #[error("Authentication error: {0}")]
27 AuthError(String),
28
29 #[error("Channel error: {0}")]
30 ChannelError(String),
31
32 #[error("Event error: {0}")]
33 EventError(String),
34
35 #[error("Connection error: {0}")]
36 ConnectionError(String),
37
38 #[error("Configuration error: {0}")]
39 ConfigError(String),
40
41 #[error("Rate limit error: {0}")]
42 RateLimitError(String),
43
44 #[error("Encryption error: {0}")]
45 EncryptionError(String),
46
47 #[error("Decryption error: {0}")]
48 DecryptionError(String),
49
50 #[error("Presence data error: {0}")]
51 PresenceDataError(String),
52
53 #[error("API error: {0}")]
54 ApiError(String),
55
56 #[error("Timeout error: {0}")]
57 TimeoutError(String),
58
59 #[error("Unknown error: {0}")]
60 UnknownError(String),
61}
62
63impl From<String> for PusherError {
64 fn from(error: String) -> Self {
65 PusherError::UnknownError(error)
66 }
67}
68
69impl From<&str> for PusherError {
70 fn from(error: &str) -> Self {
71 PusherError::UnknownError(error.to_string())
72 }
73}
74
75pub type PusherResult<T> = Result<T, PusherError>;
77
78pub fn auth_error(message: impl Into<String>) -> PusherError {
81 PusherError::AuthError(message.into())
82}
83
84pub fn channel_error(message: impl Into<String>) -> PusherError {
85 PusherError::ChannelError(message.into())
86}
87
88pub fn event_error(message: impl Into<String>) -> PusherError {
89 PusherError::EventError(message.into())
90}
91
92pub fn connection_error(message: impl Into<String>) -> PusherError {
93 PusherError::ConnectionError(message.into())
94}
95
96pub fn config_error(message: impl Into<String>) -> PusherError {
97 PusherError::ConfigError(message.into())
98}
99
100pub fn rate_limit_error(message: impl Into<String>) -> PusherError {
101 PusherError::RateLimitError(message.into())
102}
103
104pub fn encryption_error(message: impl Into<String>) -> PusherError {
105 PusherError::EncryptionError(message.into())
106}
107
108pub fn decryption_error(message: impl Into<String>) -> PusherError {
109 PusherError::DecryptionError(message.into())
110}
111
112pub fn presence_data_error(message: impl Into<String>) -> PusherError {
113 PusherError::PresenceDataError(message.into())
114}
115
116pub fn api_error(message: impl Into<String>) -> PusherError {
117 PusherError::ApiError(message.into())
118}
119
120pub fn timeout_error(message: impl Into<String>) -> PusherError {
121 PusherError::TimeoutError(message.into())
122}
123
124#[cfg(test)]
125mod tests {
126 use super::*;
127
128 #[test]
129 fn test_error_conversion() {
130 let err: PusherError = "Test error".into();
131 assert!(matches!(err, PusherError::UnknownError(_)));
132
133 let err: PusherError = String::from("Test error").into();
134 assert!(matches!(err, PusherError::UnknownError(_)));
135 }
136
137 #[test]
138 fn test_error_display() {
139 let err = PusherError::AuthError("Invalid credentials".to_string());
140 assert_eq!(err.to_string(), "Authentication error: Invalid credentials");
141
142 let err = PusherError::ChannelError("Channel not found".to_string());
143 assert_eq!(err.to_string(), "Channel error: Channel not found");
144 }
145
146 #[test]
147 fn test_helper_functions() {
148 let err = auth_error("Invalid token");
149 assert!(matches!(err, PusherError::AuthError(_)));
150
151 let err = channel_error("Invalid channel name");
152 assert!(matches!(err, PusherError::ChannelError(_)));
153
154 let err = event_error("Event too large");
155 assert!(matches!(err, PusherError::EventError(_)));
156 }
157}