google_maps_scraper_sdk/
errors.rs1use thiserror::Error;
4
5#[derive(Debug, Error)]
6pub enum Error {
7 #[error("{message}")]
8 Api {
9 message: String,
10 status_code: Option<u16>,
11 body: Option<serde_json::Value>,
12 },
13
14 #[error("{message}")]
15 Authentication {
16 message: String,
17 status_code: Option<u16>,
18 body: Option<serde_json::Value>,
19 },
20
21 #[error("{message}")]
22 PlanNotAllowed {
23 message: String,
24 status_code: Option<u16>,
25 body: Option<serde_json::Value>,
26 },
27
28 #[error("{message}")]
29 InsufficientCredits {
30 message: String,
31 status_code: Option<u16>,
32 body: Option<serde_json::Value>,
33 },
34
35 #[error("{message}")]
36 JobConflict {
37 message: String,
38 status_code: Option<u16>,
39 body: Option<serde_json::Value>,
40 },
41
42 #[error("{message}")]
43 NotFound {
44 message: String,
45 status_code: Option<u16>,
46 body: Option<serde_json::Value>,
47 },
48
49 #[error("{message}")]
50 BadRequest {
51 message: String,
52 status_code: Option<u16>,
53 body: Option<serde_json::Value>,
54 },
55
56 #[error("{message}")]
57 Timeout { message: String },
58
59 #[error(transparent)]
60 Http(#[from] reqwest::Error),
61
62 #[error(transparent)]
63 Json(#[from] serde_json::Error),
64}
65
66pub type Result<T> = std::result::Result<T, Error>;
67
68pub(crate) fn raise_for_status(status: u16, body: Option<serde_json::Value>) -> Error {
69 let message = body
70 .as_ref()
71 .and_then(|b| {
72 b.get("message")
73 .or_else(|| b.get("error"))
74 .or_else(|| b.get("statusMessage"))
75 .and_then(|v| v.as_str())
76 .map(|s| s.to_string())
77 })
78 .unwrap_or_else(|| format!("API request failed with status {status}"));
79
80 match status {
81 400 => Error::BadRequest {
82 message,
83 status_code: Some(status),
84 body,
85 },
86 401 => Error::Authentication {
87 message,
88 status_code: Some(status),
89 body,
90 },
91 402 => Error::InsufficientCredits {
92 message,
93 status_code: Some(status),
94 body,
95 },
96 403 => Error::PlanNotAllowed {
97 message,
98 status_code: Some(status),
99 body,
100 },
101 404 => Error::NotFound {
102 message,
103 status_code: Some(status),
104 body,
105 },
106 409 => Error::JobConflict {
107 message,
108 status_code: Some(status),
109 body,
110 },
111 _ => Error::Api {
112 message,
113 status_code: Some(status),
114 body,
115 },
116 }
117}