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
use crate::geocoding::response::status::Status;
#[derive(Debug)]
pub enum Error {
AddressOrComponentsRequired,
GoogleMapsService(Status, Option<String>),
HttpUnsuccessful(u8, String),
InvalidCountryCode(String),
InvalidLocationTypeCode(String),
InvalidStatusCode(String),
QueryNotBuilt,
RequestNotValidated,
Reqwest(reqwest::Error),
SerdeJson(serde_json::error::Error),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Error::AddressOrComponentsRequired => write!(f,
"Google Maps Geocoding API client: \
Forward geocoding requests must specify an `address` or at least one `component`. \
Ensure that the with_address() and/or with_component methods are being called before run()."),
Error::GoogleMapsService(status, error_message) => match error_message {
Some(error_message) => write!(f, "Google Maps Geocoding API server: {}", error_message),
None => match status {
Status::InvalidRequest => write!(f, "Google Maps Geocoding API server: \
Invalid request. \
This may indicate that the query (address, components, or latlng) is missing, an invalid result type, or an invalid location type."),
Status::Ok => write!(f, "Google Maps Geocoding server: \
Ok. \
The request was successful."),
Status::OverDailyLimit => write!(f, "Google Maps Geocoding API server: \
Over daily limit. \
Usage cap has been exceeded, API key is invalid, billing has not been enabled, or method of payment is no longer valid."),
Status::OverQueryLimit => write!(f, "Google Maps Geocoding API server: \
Over query limit. \
Requestor has exceeded quota."),
Status::RequestDenied => write!(f, "Google Maps Geocoding API server: \
Request denied \
Service did not complete the request."),
Status::UnknownError => write!(f, "Google Maps Geocoding API server: \
Unknown error."),
Status::ZeroResults => write!(f, "Google Maps Geocoding API server: \
Zero results.
This may occur if the geocoder was passed a non-existent address."),
}
},
Error::HttpUnsuccessful(retries, status) => write!(f,
"Google Maps Geocoding API client: \
Could not successfully query the Google Cloud Platform service. \
The client attempted to contact the service {} time(s). \
The service last responded with a `{}` status.", retries, status),
Error::InvalidCountryCode(country_code) => write!(f,
"Google Maps Geocoding API client: \
`{}` is not a valid ISO 3166-1 Alpha-2 country code. \
Note that the country code must be in uppercase. \
For a list of country codes see \
https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes",
country_code),
Error::InvalidLocationTypeCode(location_type_code) => write!(f,
"Google Maps Geocoding API client: \
`{}` is not a known location type code. \
Valid codes are `APPROXIMATE`, `GEOMETRIC_CENTER`, \
`RANGE_INTERPOLATED`, and `ROOFTOP`.", location_type_code),
Error::InvalidStatusCode(status_code) => write!(f,
"Google Maps Geocoding API client: \
`{}` is not a valid status code. \
Valid codes are `INVALID_REQUEST`, `OK`, `OVER_DAILY_LIMIT`, \
`OVER_QUERY_LIMIT`, `REQUEST_DENIED`, `UNKNOWN_ERROR`, and \
`ZERO_RESULTS`.", status_code),
Error::QueryNotBuilt => write!(f,
"Google Maps Geocoding API client: \
The query string must be built before the request may be sent to the Google Cloud Maps Platform. \
Ensure the build() method is called before run()."),
Error::RequestNotValidated => write!(f,
"Google Maps Geocoding API client: \
The request must be validated before a query string may be built. \
Ensure the validate() method is called before build()."),
Error::Reqwest(error) => write!(f, "Google Maps Geocoding API client in the Reqwest library: {}", error),
Error::SerdeJson(error) => write!(f, "Google Maps Geocoding API client in the Serde JSON library: {}", error),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::AddressOrComponentsRequired => None,
Error::GoogleMapsService(_error, _message) => None,
Error::HttpUnsuccessful(_retries, _status) => None,
Error::InvalidCountryCode(_country_code) => None,
Error::InvalidLocationTypeCode(_location_type_code) => None,
Error::InvalidStatusCode(_status_code) => None,
Error::QueryNotBuilt => None,
Error::RequestNotValidated => None,
Error::Reqwest(error) => Some(error),
Error::SerdeJson(error) => Some(error),
}
}
}
impl From<reqwest::Error> for Error {
fn from(error: reqwest::Error) -> Error {
Error::Reqwest(error)
}
}
impl From<serde_json::error::Error> for Error {
fn from(error: serde_json::error::Error) -> Error {
Error::SerdeJson(error)
}
}