Skip to main content

chaser_cf/
error.rs

1//! Error types for chaser-cf
2
3/// Result type alias for chaser-cf operations
4pub type ChaserResult<T> = Result<T, ChaserError>;
5
6/// chaser-cf error types
7#[derive(Debug, thiserror::Error)]
8pub enum ChaserError {
9    /// Browser not initialized
10    #[error("Browser not initialized. Call init() first or use lazy initialization.")]
11    NotInitialized,
12
13    /// Browser initialization failed
14    #[error("Failed to initialize browser: {0}")]
15    InitFailed(String),
16
17    /// Browser context creation failed
18    #[error("Failed to create browser context: {0}")]
19    ContextFailed(String),
20
21    /// Page creation failed
22    #[error("Failed to create page: {0}")]
23    PageFailed(String),
24
25    /// Navigation failed
26    #[error("Navigation failed: {0}")]
27    NavigationFailed(String),
28
29    /// Operation timed out
30    #[error("Operation timed out after {0}ms")]
31    Timeout(u64),
32
33    /// Captcha solving failed
34    #[error("Failed to solve captcha: {0}")]
35    CaptchaFailed(String),
36
37    /// Token extraction failed
38    #[error("Failed to extract token")]
39    TokenExtractionFailed,
40
41    /// Cookie extraction failed
42    #[error("Failed to extract cookies: {0}")]
43    CookieExtractionFailed(String),
44
45    /// Invalid configuration
46    #[error("Invalid configuration: {0}")]
47    InvalidConfig(String),
48
49    /// Invalid URL
50    #[error("Invalid URL: {0}")]
51    InvalidUrl(String),
52
53    /// Missing required parameter
54    #[error("Missing required parameter: {0}")]
55    MissingParameter(String),
56
57    /// Proxy error
58    #[error("Proxy error: {0}")]
59    ProxyError(String),
60
61    /// Internal error
62    #[error("Internal error: {0}")]
63    Internal(String),
64}
65
66impl ChaserError {
67    /// Get error code for FFI
68    pub fn code(&self) -> i32 {
69        match self {
70            ChaserError::NotInitialized => 1,
71            ChaserError::InitFailed(_) => 2,
72            ChaserError::ContextFailed(_) => 3,
73            ChaserError::PageFailed(_) => 4,
74            ChaserError::NavigationFailed(_) => 5,
75            ChaserError::Timeout(_) => 6,
76            ChaserError::CaptchaFailed(_) => 7,
77            ChaserError::TokenExtractionFailed => 8,
78            ChaserError::CookieExtractionFailed(_) => 9,
79            ChaserError::InvalidConfig(_) => 10,
80            ChaserError::InvalidUrl(_) => 11,
81            ChaserError::MissingParameter(_) => 12,
82            ChaserError::ProxyError(_) => 13,
83            ChaserError::Internal(_) => 99,
84        }
85    }
86}
87
88impl From<anyhow::Error> for ChaserError {
89    fn from(err: anyhow::Error) -> Self {
90        ChaserError::Internal(err.to_string())
91    }
92}
93
94impl From<tokio::time::error::Elapsed> for ChaserError {
95    fn from(_: tokio::time::error::Elapsed) -> Self {
96        ChaserError::Timeout(0)
97    }
98}
99
100impl From<url::ParseError> for ChaserError {
101    fn from(err: url::ParseError) -> Self {
102        ChaserError::InvalidUrl(err.to_string())
103    }
104}