1pub type ChaserResult<T> = Result<T, ChaserError>;
5
6#[derive(Debug, thiserror::Error)]
8pub enum ChaserError {
9 #[error("Browser not initialized. Call init() first or use lazy initialization.")]
11 NotInitialized,
12
13 #[error("Failed to initialize browser: {0}")]
15 InitFailed(String),
16
17 #[error("Failed to create browser context: {0}")]
19 ContextFailed(String),
20
21 #[error("Failed to create page: {0}")]
23 PageFailed(String),
24
25 #[error("Navigation failed: {0}")]
27 NavigationFailed(String),
28
29 #[error("Operation timed out after {0}ms")]
31 Timeout(u64),
32
33 #[error("Failed to solve captcha: {0}")]
35 CaptchaFailed(String),
36
37 #[error("Failed to extract token")]
39 TokenExtractionFailed,
40
41 #[error("Failed to extract cookies: {0}")]
43 CookieExtractionFailed(String),
44
45 #[error("Invalid configuration: {0}")]
47 InvalidConfig(String),
48
49 #[error("Invalid URL: {0}")]
51 InvalidUrl(String),
52
53 #[error("Missing required parameter: {0}")]
55 MissingParameter(String),
56
57 #[error("Proxy error: {0}")]
59 ProxyError(String),
60
61 #[error("Internal error: {0}")]
63 Internal(String),
64}
65
66impl ChaserError {
67 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}