Skip to main content

mcp_gmailcal/
errors.rs

1use reqwest;
2use std::env;
3use thiserror::Error;
4
5/// Error type for configuration issues
6#[derive(Debug, Error)]
7pub enum ConfigError {
8    #[error("Missing environment variable: {0}")]
9    MissingEnvVar(String),
10
11    #[error("Environment error: {0}")]
12    EnvError(#[from] env::VarError),
13}
14
15/// General application errors
16#[derive(Debug, Error)]
17pub enum AppError {
18    #[error("Cache is disabled")]
19    CacheDisabled,
20    
21    #[error("IO Error: {0}")]
22    IoError(String),
23    
24    #[error("Encryption error: {0}")]
25    EncryptionError(String),
26}
27
28/// Error type for Gmail API operations
29#[derive(Debug, Error)]
30pub enum GmailApiError {
31    #[error("Gmail API error: {0}")]
32    ApiError(String),
33
34    #[error("Authentication error: {0}")]
35    AuthError(String),
36
37    #[error("Message retrieval error: {0}")]
38    MessageRetrievalError(String),
39
40    #[error("Message format error: {0}")]
41    MessageFormatError(String),
42
43    #[error("Network error: {0}")]
44    NetworkError(String),
45
46    #[error("Rate limit error: {0}")]
47    RateLimitError(String),
48    
49    #[error("Token cache error: {0}")]
50    CacheError(String),
51}
52
53/// Type alias for Gmail API results
54pub type GmailResult<T> = std::result::Result<T, GmailApiError>;
55
56/// Error type for People API operations
57#[derive(Debug, Error)]
58pub enum PeopleApiError {
59    #[error("Network error: {0}")]
60    NetworkError(String),
61
62    #[error("Authentication error: {0}")]
63    AuthError(String),
64
65    #[error("People API error: {0}")]
66    ApiError(String),
67
68    #[error("Invalid input: {0}")]
69    InvalidInput(String),
70
71    #[error("Parse error: {0}")]
72    ParseError(String),
73}
74
75/// Type alias for People API results
76pub type PeopleResult<T> = std::result::Result<T, PeopleApiError>;
77
78/// Error type for Calendar API operations
79#[derive(Debug, Error)]
80pub enum CalendarApiError {
81    #[error("Calendar API error: {0}")]
82    ApiError(String),
83
84    #[error("Authentication error: {0}")]
85    AuthError(String),
86
87    #[error("Event retrieval error: {0}")]
88    EventRetrievalError(String),
89
90    #[error("Event format error: {0}")]
91    EventFormatError(String),
92
93    #[error("Network error: {0}")]
94    NetworkError(String),
95
96    #[error("Rate limit error: {0}")]
97    RateLimitError(String),
98
99    #[error("Parse error: {0}")]
100    ParseError(String),
101}
102
103/// Type alias for Calendar API results
104pub type CalendarResult<T> = std::result::Result<T, CalendarApiError>;
105
106/// MCP error codes for different error scenarios
107pub mod error_codes {
108    // General errors
109    pub const INTERNAL_ERROR: &str = "internal_error";
110    pub const AUTHENTICATION_ERROR: &str = "authentication_error";
111    pub const NOT_FOUND_ERROR: &str = "not_found_error";
112    pub const INVALID_REQUEST: &str = "invalid_request";
113    pub const RATE_LIMIT_ERROR: &str = "rate_limit_error";
114    pub const CONFIG_ERROR: &str = "config_error";
115
116    // Gmail specific errors
117    pub const MESSAGE_NOT_FOUND: &str = "message_not_found";
118    pub const DRAFT_NOT_FOUND: &str = "draft_not_found";
119    pub const MESSAGE_FORMAT_ERROR: &str = "message_format_error";
120
121    // Calendar specific errors
122    pub const CALENDAR_NOT_FOUND: &str = "calendar_not_found";
123    pub const EVENT_NOT_FOUND: &str = "event_not_found";
124    pub const EVENT_FORMAT_ERROR: &str = "event_format_error";
125
126    // People API specific errors
127    pub const CONTACT_NOT_FOUND: &str = "contact_not_found";
128}
129
130// From implementations for error conversion
131impl From<reqwest::Error> for GmailApiError {
132    fn from(err: reqwest::Error) -> Self {
133        GmailApiError::NetworkError(format!("Network error: {}", err))
134    }
135}
136
137impl From<reqwest::Error> for PeopleApiError {
138    fn from(err: reqwest::Error) -> Self {
139        PeopleApiError::NetworkError(format!("Network error: {}", err))
140    }
141}
142
143impl From<reqwest::Error> for CalendarApiError {
144    fn from(err: reqwest::Error) -> Self {
145        CalendarApiError::NetworkError(format!("Network error: {}", err))
146    }
147}