ceres-core 0.4.0

Core types, harvesting logic, and services for Ceres
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
use thiserror::Error;

/// Application-wide error types.
///
/// This enum represents all possible errors that can occur in the Ceres application.
/// It uses the `thiserror` crate for ergonomic error handling and automatic conversion
/// from underlying library errors.
///
/// # Error Conversion
///
/// Most errors automatically convert from their source types using the `#[from]` attribute:
/// - `serde_json::Error` → `AppError::SerializationError`
///
/// Database errors are converted explicitly in the database layer.
///
/// # Examples
///
/// ```no_run
/// use ceres_core::error::AppError;
///
/// fn example() -> Result<(), AppError> {
///     // Errors automatically convert
///     Err(AppError::Generic("Something went wrong".to_string()))
/// }
/// ```
///
/// # Gemini Error Classification
///
/// Gemini API errors are classified into specific categories for better error handling.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GeminiErrorKind {
    /// Authentication failure (401, invalid API key)
    Authentication,
    /// Rate limit exceeded (429)
    RateLimit,
    /// Quota exceeded (insufficient_quota)
    QuotaExceeded,
    /// Server error (5xx)
    ServerError,
    /// Network/connection error
    NetworkError,
    /// Unknown or unclassified error
    Unknown,
}

/// Structured error details from Gemini API
#[derive(Debug, Clone)]
pub struct GeminiErrorDetails {
    /// The specific error category
    pub kind: GeminiErrorKind,
    /// Human-readable error message from the API
    pub message: String,
    /// HTTP status code
    pub status_code: u16,
}

impl GeminiErrorDetails {
    /// Create a new GeminiErrorDetails
    pub fn new(kind: GeminiErrorKind, message: String, status_code: u16) -> Self {
        Self {
            kind,
            message,
            status_code,
        }
    }
}

impl std::fmt::Display for GeminiErrorDetails {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Gemini API error (HTTP {}): {}",
            self.status_code, self.message
        )
    }
}

#[derive(Error, Debug)]
pub enum AppError {
    /// Database operation failed.
    ///
    /// This error wraps database operation failures as plain strings,
    /// keeping the domain layer independent of any specific database library.
    #[error("Database error: {0}")]
    DatabaseError(String),

    /// HTTP client request failed.
    ///
    /// This error occurs when HTTP requests fail due to network issues,
    /// timeouts, or server errors.
    #[error("API Client error: {0}")]
    ClientError(String),

    /// Gemini API call failed.
    ///
    /// This error occurs when Gemini API calls fail, including
    /// authentication failures, rate limiting, and API errors.
    /// Contains structured error information for better error handling.
    #[error("Gemini error: {0}")]
    GeminiError(GeminiErrorDetails),

    /// JSON serialization or deserialization failed.
    ///
    /// This error occurs when converting between Rust types and JSON,
    /// typically when parsing API responses or preparing database values.
    #[error("Serialization error: {0}")]
    SerializationError(#[from] serde_json::Error),

    /// URL parsing failed.
    ///
    /// This error occurs when attempting to parse an invalid URL string,
    /// typically when constructing API endpoints or validating portal URLs.
    #[error("Invalid URL: {0}")]
    InvalidUrl(String),

    /// Dataset not found in the database.
    ///
    /// This error indicates that a requested dataset does not exist.
    #[error("Dataset not found: {0}")]
    DatasetNotFound(String),

    /// Invalid CKAN portal URL provided.
    ///
    /// This error occurs when the provided CKAN portal URL is malformed
    /// or cannot be used to construct valid API endpoints.
    #[error("Invalid CKAN portal URL: {0}")]
    InvalidPortalUrl(String),

    /// API response contained no data.
    ///
    /// This error occurs when an API returns a successful status but
    /// the response body is empty or missing expected data.
    #[error("Empty response from API")]
    EmptyResponse,

    /// Network or connection error.
    ///
    /// This error occurs when a network request fails due to connectivity issues,
    /// DNS resolution failures, or the remote server being unreachable.
    #[error("Network error: {0}")]
    NetworkError(String),

    /// Request timeout.
    ///
    /// This error occurs when a request takes longer than the configured timeout.
    #[error("Request timed out after {0} seconds")]
    Timeout(u64),

    /// Rate limit exceeded.
    ///
    /// This error occurs when too many requests are made in a short period.
    #[error("Rate limit exceeded. Please wait and try again.")]
    RateLimitExceeded,

    /// Configuration file error.
    ///
    /// This error occurs when reading or parsing the configuration file fails,
    /// such as when the portals.toml file is malformed or contains invalid values.
    #[error("Configuration error: {0}")]
    ConfigError(String),

    /// Filesystem I/O error.
    ///
    /// This error wraps `std::io::Error` failures from reading or writing files,
    /// typically in export paths (creating output directories, writing files).
    #[error("I/O error: {0}")]
    IoError(String),

    /// Data export error.
    ///
    /// This error covers failures specific to the export pipeline that are not
    /// plain I/O — e.g. Arrow/Parquet schema or serialization failures.
    #[error("Export error: {0}")]
    ExportError(String),

    /// Generic application error for cases not covered by specific variants.
    ///
    /// Use this sparingly - prefer creating specific error variants
    /// for better error handling and debugging.
    #[error("Error: {0}")]
    Generic(String),
}

impl AppError {
    /// Returns a user-friendly error message suitable for CLI output.
    pub fn user_message(&self) -> String {
        match self {
            AppError::DatabaseError(e) => {
                if e.to_string().contains("connection") {
                    "Cannot connect to database. Is PostgreSQL running?\n   Try: docker-compose up -d".to_string()
                } else {
                    format!("Database error: {}", e)
                }
            }
            AppError::ClientError(msg) => {
                if msg.contains("timeout") || msg.contains("timed out") {
                    "Request timed out. The portal may be slow or unreachable.\n   Try again later or check the portal URL.".to_string()
                } else if msg.contains("connect") {
                    format!("Cannot connect to portal: {}\n   Check your internet connection and the portal URL.", msg)
                } else {
                    format!("API error: {}", msg)
                }
            }
            AppError::GeminiError(details) => match details.kind {
                GeminiErrorKind::Authentication => {
                    "Invalid Gemini API key.\n   Check your GEMINI_API_KEY environment variable."
                        .to_string()
                }
                GeminiErrorKind::RateLimit => {
                    "Gemini rate limit reached.\n   Wait a moment and try again, or reduce concurrency."
                        .to_string()
                }
                GeminiErrorKind::QuotaExceeded => {
                    "Gemini quota exceeded.\n   Check your Google account billing.".to_string()
                }
                GeminiErrorKind::ServerError => {
                    format!(
                        "Gemini server error (HTTP {}).\n   Please try again later.",
                        details.status_code
                    )
                }
                GeminiErrorKind::NetworkError => {
                    format!(
                        "Network error connecting to Gemini: {}\n   Check your internet connection.",
                        details.message
                    )
                }
                GeminiErrorKind::Unknown => {
                    format!("Gemini error: {}", details.message)
                }
            },
            AppError::InvalidPortalUrl(url) => {
                format!(
                    "Invalid portal URL: {}\n   Example: https://dati.comune.milano.it",
                    url
                )
            }
            AppError::NetworkError(msg) => {
                format!("Network error: {}\n   Check your internet connection.", msg)
            }
            AppError::Timeout(secs) => {
                format!("Request timed out after {} seconds.\n   The server may be overloaded. Try again later.", secs)
            }
            AppError::RateLimitExceeded => {
                "Too many requests. Please wait a moment and try again.".to_string()
            }
            AppError::EmptyResponse => {
                "The API returned no data. The portal may be temporarily unavailable.".to_string()
            }
            AppError::ConfigError(msg) => {
                format!(
                    "Configuration error: {}\n   Check your configuration file.",
                    msg
                )
            }
            _ => self.to_string(),
        }
    }

    /// Returns true if this error is retryable.
    ///
    /// # Examples
    ///
    /// ```
    /// use ceres_core::error::AppError;
    ///
    /// // Network errors are retryable
    /// let err = AppError::NetworkError("connection reset".to_string());
    /// assert!(err.is_retryable());
    ///
    /// // Rate limits are retryable (after a delay)
    /// let err = AppError::RateLimitExceeded;
    /// assert!(err.is_retryable());
    ///
    /// // Dataset not found is NOT retryable
    /// let err = AppError::DatasetNotFound("test".to_string());
    /// assert!(!err.is_retryable());
    /// ```
    pub fn is_retryable(&self) -> bool {
        match self {
            AppError::NetworkError(_)
            | AppError::Timeout(_)
            | AppError::RateLimitExceeded
            | AppError::ClientError(_) => true,
            AppError::GeminiError(details) => matches!(
                details.kind,
                GeminiErrorKind::RateLimit
                    | GeminiErrorKind::NetworkError
                    | GeminiErrorKind::ServerError
            ),
            _ => false,
        }
    }

    /// Returns true if this error should trip the circuit breaker.
    ///
    /// Transient errors (network issues, timeouts, rate limits, server errors)
    /// should trip the circuit breaker. Non-transient errors (authentication,
    /// quota exceeded, invalid data) should NOT trip the circuit.
    ///
    /// # Examples
    ///
    /// ```
    /// use ceres_core::error::{AppError, GeminiErrorKind, GeminiErrorDetails};
    ///
    /// // Network errors trip the circuit
    /// let err = AppError::NetworkError("connection reset".to_string());
    /// assert!(err.should_trip_circuit());
    ///
    /// // Authentication errors do NOT trip the circuit
    /// let err = AppError::GeminiError(GeminiErrorDetails::new(
    ///     GeminiErrorKind::Authentication,
    ///     "Invalid API key".to_string(),
    ///     401,
    /// ));
    /// assert!(!err.should_trip_circuit());
    /// ```
    pub fn should_trip_circuit(&self) -> bool {
        match self {
            // Transient errors - should trip circuit
            AppError::NetworkError(_) | AppError::Timeout(_) | AppError::RateLimitExceeded => true,

            // Client errors are often transient (timeouts, connection issues)
            AppError::ClientError(msg) => {
                msg.contains("timeout")
                    || msg.contains("timed out")
                    || msg.contains("connect")
                    || msg.contains("connection")
            }

            // Gemini errors - only transient ones
            AppError::GeminiError(details) => matches!(
                details.kind,
                GeminiErrorKind::RateLimit
                    | GeminiErrorKind::NetworkError
                    | GeminiErrorKind::ServerError
            ),

            // Non-transient errors - should NOT trip circuit
            // Authentication, quota, validation errors indicate configuration
            // problems, not temporary service issues
            AppError::DatabaseError(_)
            | AppError::SerializationError(_)
            | AppError::InvalidUrl(_)
            | AppError::DatasetNotFound(_)
            | AppError::InvalidPortalUrl(_)
            | AppError::EmptyResponse
            | AppError::ConfigError(_)
            | AppError::IoError(_)
            | AppError::ExportError(_)
            | AppError::Generic(_) => false,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_error_display() {
        let err = AppError::DatasetNotFound("test-id".to_string());
        assert_eq!(err.to_string(), "Dataset not found: test-id");
    }

    #[test]
    fn test_generic_error() {
        let err = AppError::Generic("Something went wrong".to_string());
        assert_eq!(err.to_string(), "Error: Something went wrong");
    }

    #[test]
    fn test_empty_response_error() {
        let err = AppError::EmptyResponse;
        assert_eq!(err.to_string(), "Empty response from API");
    }

    #[test]
    fn test_user_message_gemini_auth() {
        let details = GeminiErrorDetails::new(
            GeminiErrorKind::Authentication,
            "Invalid API key".to_string(),
            401,
        );
        let err = AppError::GeminiError(details);
        let msg = err.user_message();
        assert!(msg.contains("Invalid Gemini API key"));
        assert!(msg.contains("GEMINI_API_KEY"));
    }

    #[test]
    fn test_user_message_gemini_rate_limit() {
        let details = GeminiErrorDetails::new(
            GeminiErrorKind::RateLimit,
            "Rate limit exceeded".to_string(),
            429,
        );
        let err = AppError::GeminiError(details);
        let msg = err.user_message();
        assert!(msg.contains("rate limit"));
    }

    #[test]
    fn test_user_message_gemini_quota() {
        let details = GeminiErrorDetails::new(
            GeminiErrorKind::QuotaExceeded,
            "Insufficient quota".to_string(),
            429,
        );
        let err = AppError::GeminiError(details);
        let msg = err.user_message();
        assert!(msg.contains("quota exceeded"));
        assert!(msg.contains("Google account billing"));
    }

    #[test]
    fn test_gemini_error_display() {
        let details = GeminiErrorDetails::new(
            GeminiErrorKind::Authentication,
            "Invalid API key".to_string(),
            401,
        );
        let err = AppError::GeminiError(details);
        assert!(err.to_string().contains("Gemini error"));
        assert!(err.to_string().contains("401"));
    }

    #[test]
    fn test_gemini_error_retryable() {
        let rate_limit = AppError::GeminiError(GeminiErrorDetails::new(
            GeminiErrorKind::RateLimit,
            "Rate limit".to_string(),
            429,
        ));
        assert!(rate_limit.is_retryable());

        let auth_error = AppError::GeminiError(GeminiErrorDetails::new(
            GeminiErrorKind::Authentication,
            "Invalid key".to_string(),
            401,
        ));
        assert!(!auth_error.is_retryable());

        let server_error = AppError::GeminiError(GeminiErrorDetails::new(
            GeminiErrorKind::ServerError,
            "Internal server error".to_string(),
            500,
        ));
        assert!(server_error.is_retryable());
    }

    #[test]
    fn test_invalid_portal_url() {
        let err = AppError::InvalidPortalUrl("not a url".to_string());
        assert!(err.to_string().contains("Invalid CKAN portal URL"));
    }

    #[test]
    fn test_error_from_serde() {
        let json = "{ invalid json }";
        let result: Result<serde_json::Value, _> = serde_json::from_str(json);
        let serde_err = result.unwrap_err();
        let app_err: AppError = serde_err.into();
        assert!(matches!(app_err, AppError::SerializationError(_)));
    }

    #[test]
    fn test_user_message_database_connection() {
        let err = AppError::DatabaseError("Pool timed out: connection".to_string());
        let msg = err.user_message();
        assert!(msg.contains("Cannot connect to database"));
    }

    #[test]
    fn test_is_retryable() {
        assert!(AppError::NetworkError("timeout".to_string()).is_retryable());
        assert!(AppError::Timeout(30).is_retryable());
        assert!(AppError::RateLimitExceeded.is_retryable());
        assert!(!AppError::InvalidPortalUrl("bad".to_string()).is_retryable());
    }

    #[test]
    fn test_timeout_error() {
        let err = AppError::Timeout(30);
        assert_eq!(err.to_string(), "Request timed out after 30 seconds");
    }

    #[test]
    fn test_should_trip_circuit_transient_errors() {
        // Transient errors should trip the circuit
        assert!(AppError::NetworkError("connection reset".to_string()).should_trip_circuit());
        assert!(AppError::Timeout(30).should_trip_circuit());
        assert!(AppError::RateLimitExceeded.should_trip_circuit());
    }

    #[test]
    fn test_should_trip_circuit_client_errors() {
        // Client errors with transient keywords should trip
        assert!(AppError::ClientError("connection refused".to_string()).should_trip_circuit());
        assert!(AppError::ClientError("request timed out".to_string()).should_trip_circuit());

        // Client errors without transient keywords should NOT trip
        assert!(!AppError::ClientError("invalid json".to_string()).should_trip_circuit());
    }

    #[test]
    fn test_should_trip_circuit_gemini_errors() {
        // Rate limit should trip
        let rate_limit = AppError::GeminiError(GeminiErrorDetails::new(
            GeminiErrorKind::RateLimit,
            "Rate limit exceeded".to_string(),
            429,
        ));
        assert!(rate_limit.should_trip_circuit());

        // Server error should trip
        let server_error = AppError::GeminiError(GeminiErrorDetails::new(
            GeminiErrorKind::ServerError,
            "Internal server error".to_string(),
            500,
        ));
        assert!(server_error.should_trip_circuit());

        // Network error should trip
        let network_error = AppError::GeminiError(GeminiErrorDetails::new(
            GeminiErrorKind::NetworkError,
            "Connection failed".to_string(),
            0,
        ));
        assert!(network_error.should_trip_circuit());

        // Authentication should NOT trip
        let auth_error = AppError::GeminiError(GeminiErrorDetails::new(
            GeminiErrorKind::Authentication,
            "Invalid API key".to_string(),
            401,
        ));
        assert!(!auth_error.should_trip_circuit());

        // Quota exceeded should NOT trip
        let quota_error = AppError::GeminiError(GeminiErrorDetails::new(
            GeminiErrorKind::QuotaExceeded,
            "Insufficient quota".to_string(),
            429,
        ));
        assert!(!quota_error.should_trip_circuit());
    }

    #[test]
    fn test_should_trip_circuit_non_transient_errors() {
        // These should NOT trip the circuit
        assert!(!AppError::InvalidPortalUrl("bad url".to_string()).should_trip_circuit());
        assert!(!AppError::DatasetNotFound("missing".to_string()).should_trip_circuit());
        assert!(!AppError::InvalidUrl("bad".to_string()).should_trip_circuit());
        assert!(!AppError::EmptyResponse.should_trip_circuit());
        assert!(!AppError::ConfigError("bad config".to_string()).should_trip_circuit());
        assert!(!AppError::Generic("something".to_string()).should_trip_circuit());
    }
}