1use crate::internal_imap as imap_rs;
9use imap_rs::client::ClientError;
10use serde::{Deserialize, Serialize};
11use thiserror::Error;
12
13#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
19#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
20#[non_exhaustive]
21pub enum ErrorCode {
22 Network,
24 Tls,
26 Auth,
28 UnsupportedAuth,
30 Crypto,
32 InvalidRequest,
34 NotFound,
36 Cancelled,
38 Server,
40 Internal,
42}
43
44#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
50#[serde(rename_all = "camelCase")]
51pub struct MailError {
52 pub code: ErrorCode,
54 pub message: String,
56}
57
58#[derive(Debug, Error)]
64#[non_exhaustive]
65pub enum Error {
66 #[error("invalid request: {0}")]
68 InvalidRequest(String),
69 #[error("session not found")]
71 NotFound,
72 #[error("operation cancelled")]
74 Cancelled,
75 #[error("authentication is not supported by this server or crate path: {0}")]
77 UnsupportedAuth(String),
78 #[error("authentication failed: {0}")]
80 Auth(String),
81 #[error("crypto error: {0}")]
83 Crypto(String),
84 #[error("network error: {0}")]
86 Network(String),
87 #[error("server error: {0}")]
89 Server(String),
90 #[error("internal error: {0}")]
92 Internal(String),
93}
94
95impl Error {
96 #[must_use]
98 pub fn code(&self) -> ErrorCode {
99 match self {
100 Self::InvalidRequest(_) => ErrorCode::InvalidRequest,
101 Self::NotFound => ErrorCode::NotFound,
102 Self::Cancelled => ErrorCode::Cancelled,
103 Self::UnsupportedAuth(_) => ErrorCode::UnsupportedAuth,
104 Self::Auth(_) => ErrorCode::Auth,
105 Self::Crypto(_) => ErrorCode::Crypto,
106 Self::Network(_) => ErrorCode::Network,
107 Self::Server(_) => ErrorCode::Server,
108 Self::Internal(_) => ErrorCode::Internal,
109 }
110 }
111
112 #[must_use]
116 pub fn public(self) -> MailError {
117 MailError {
118 code: self.code(),
119 message: self.to_string(),
120 }
121 }
122}
123
124#[doc(hidden)]
128pub type MailJniError = Error;
129
130#[doc(hidden)]
131#[must_use]
132#[allow(clippy::needless_pass_by_value)] pub fn map_imap_error(error: ClientError) -> MailJniError {
134 if is_reconnectable_imap_error(&error) {
135 return MailJniError::Network(error.to_string());
136 }
137
138 if matches!(error, ClientError::UnsupportedCapability(_)) {
139 return MailJniError::UnsupportedAuth(error.to_string());
140 }
141
142 let message = error.to_string();
143 if message.to_ascii_lowercase().contains("auth")
144 || message.to_ascii_lowercase().contains("login")
145 {
146 MailJniError::Auth(message)
147 } else if message.to_ascii_lowercase().contains("unsupported") {
148 MailJniError::UnsupportedAuth(message)
149 } else if message.to_ascii_lowercase().contains("timeout") {
150 MailJniError::Network(message)
151 } else {
152 MailJniError::Server(message)
153 }
154}
155
156#[doc(hidden)]
157#[must_use]
158pub fn is_reconnectable_imap_error(error: &ClientError) -> bool {
159 matches!(
160 error,
161 ClientError::Timeout | ClientError::ConnectionClosed | ClientError::Io(_)
162 )
163}
164
165#[cfg(test)]
166mod tests {
167 use super::*;
168
169 #[test]
170 fn public_error_maps_each_variant_to_stable_code_and_message() {
171 let cases = vec![
172 (
173 MailJniError::InvalidRequest("bad".to_string()),
174 ErrorCode::InvalidRequest,
175 ),
176 (MailJniError::NotFound, ErrorCode::NotFound),
177 (MailJniError::Cancelled, ErrorCode::Cancelled),
178 (
179 MailJniError::UnsupportedAuth("xoauth2".to_string()),
180 ErrorCode::UnsupportedAuth,
181 ),
182 (MailJniError::Auth("denied".to_string()), ErrorCode::Auth),
183 (
184 MailJniError::Crypto("bad signature".to_string()),
185 ErrorCode::Crypto,
186 ),
187 (
188 MailJniError::Network("offline".to_string()),
189 ErrorCode::Network,
190 ),
191 (MailJniError::Server("boom".to_string()), ErrorCode::Server),
192 (
193 MailJniError::Internal("broken".to_string()),
194 ErrorCode::Internal,
195 ),
196 ];
197
198 for (error, code) in cases {
199 let message = error.to_string();
200 let public = error.public();
201 assert_eq!(public.code, code);
202 assert_eq!(public.message, message);
203 }
204 }
205
206 #[test]
207 fn imap_error_mapping_covers_auth_network_and_server_fallbacks() {
208 assert!(matches!(
209 map_imap_error(ClientError::CommandFailed(
210 "AUTHENTICATE failed".to_string()
211 )),
212 MailJniError::Auth(_)
213 ));
214 assert!(matches!(
215 map_imap_error(ClientError::Timeout),
216 MailJniError::Network(_)
217 ));
218 assert!(matches!(
219 map_imap_error(ClientError::UnsupportedCapability("IDLE")),
220 MailJniError::UnsupportedAuth(_)
221 ));
222 assert!(matches!(
223 map_imap_error(ClientError::CommandFailed("NO mailbox missing".to_string())),
224 MailJniError::Server(_)
225 ));
226 }
227}