1#[derive(Debug, thiserror::Error)]
5#[non_exhaustive]
6pub enum Error {
7 #[error("{0}")]
10 InvalidTask(String),
11
12 #[error("network error: {0}")]
14 Network(#[from] reqwest::Error),
15
16 #[error("could not parse the API response: {message}; raw response: {raw}")]
18 BadResponse {
19 message: String,
20 raw: String,
22 },
23
24 #[error("API error {error_id} {error_code}: {description}")]
27 Api {
28 error_id: i64,
29 error_code: String,
30 description: String,
31 },
32
33 #[error("the task was not solved in {0} seconds")]
35 Timeout(u64),
36
37 #[error("could not read {path}: {source}")]
39 Io {
40 path: String,
41 #[source]
42 source: std::io::Error,
43 },
44}
45
46impl Error {
47 pub fn api_error_code(&self) -> Option<&str> {
49 match self {
50 Error::Api { error_code, .. } => Some(error_code),
51 _ => None,
52 }
53 }
54
55 pub fn is_retryable(&self) -> bool {
57 match self {
58 Error::Network(_) | Error::Timeout(_) => true,
59 Error::Api { error_code, .. } => matches!(
60 error_code.as_str(),
61 "ERROR_NO_SLOT_AVAILABLE"
62 | "ERROR_SERVICE_OVERLOAD"
63 | "ERROR_RATE_LIMIT"
64 | "ERROR_CAPTCHA_UNSOLVABLE"
65 ),
66 _ => false,
67 }
68 }
69}
70
71pub type Result<T> = std::result::Result<T, Error>;