google_maps2/geocoding/
error.rs1use crate::geocoding::response::status::Status;
6use miette::Diagnostic;
7use thiserror::Error;
8
9#[derive(Debug, Diagnostic, Error)]
14#[diagnostic(code(google_maps::geocoding::error), url(docsrs))]
15pub enum Error {
16    AddressOrComponentsRequired,
19    GoogleMapsService(Status, Option<String>),
22    HttpUnsuccessful(String),
24    InvalidStatusCode(String),
27    QueryNotBuilt,
30    RequestNotValidated,
32    #[cfg(feature = "enable-reqwest")]
34    Reqwest(crate::ReqError),
35    #[cfg(feature = "enable-reqwest")]
38    ReqwestMessage(String),
39    SerdeJson(serde_json::error::Error),
41} impl std::fmt::Display for Error {
44    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
47        match self {
48            Self::AddressOrComponentsRequired => write!(f,
49                "Google Maps Geocoding API client: \
50                Forward geocoding requests must specify an `address` or at least one `component`. \
51                Ensure that the with_address() and/or with_component methods are being called before run()."),
52            Self::GoogleMapsService(status, error_message) => match error_message {
53                Some(error_message) => write!(f, "Google Maps Geocoding API server: {error_message}"),
56                None => match status {
60                    Status::InvalidRequest => write!(f, "Google Maps Geocoding API server: \
61                        Invalid request. \
62                        This may indicate that the query (address, components, or latlng) is missing, an invalid result type, or an invalid location type."),
63                    Status::Ok => write!(f, "Google Maps Geocoding server: \
64                        Ok. \
65                        The request was successful."),
66                    Status::OverDailyLimit => write!(f, "Google Maps Geocoding API server: \
67                        Over daily limit. \
68                        Usage cap has been exceeded, API key is invalid, billing has not been enabled, or method of payment is no longer valid."),
69                    Status::OverQueryLimit => write!(f, "Google Maps Geocoding API server: \
70                        Over query limit. \
71                        Requestor has exceeded quota."),
72                    Status::RequestDenied => write!(f, "Google Maps Geocoding API server: \
73                        Request denied \
74                        Service did not complete the request."),
75                    Status::UnknownError => write!(f, "Google Maps Geocoding API server: \
76                        Unknown error."),
77                    Status::ZeroResults => write!(f, "Google Maps Geocoding API server: \
78                        Zero results. \
79                        This may occur if the geocoder was passed a non-existent address."),
80                } }, Self::HttpUnsuccessful(status) => write!(f,
83                "Google Maps Geocoding API client: \
84                Could not successfully query the Google Cloud Platform service. \
85                The service last responded with a `{status}` status."),
86            Self::InvalidStatusCode(status_code) => write!(f,
87                "Google Maps Geocoding API client: \
88                `{status_code}` is not a valid status code. \
89                Valid codes are `INVALID_REQUEST`, `OK`, `OVER_DAILY_LIMIT`, \
90                `OVER_QUERY_LIMIT`, `REQUEST_DENIED`, `UNKNOWN_ERROR`, and \
91                `ZERO_RESULTS`."),
92            Self::QueryNotBuilt => write!(f,
93                "Google Maps Geocoding API client: \
94                The query string must be built before the request may be sent to the Google Cloud Maps Platform. \
95                Ensure the build() method is called before run()."),
96            Self::RequestNotValidated => write!(f,
97                "Google Maps Geocoding API client: \
98                The request must be validated before a query string may be built. \
99                Ensure the validate() method is called before build()."),
100            #[cfg(feature = "enable-reqwest")]
101            Self::Reqwest(error) => write!(f, "Google Maps Geocoding API client in the Reqwest library: {error}"),
102            #[cfg(feature = "enable-reqwest")]
103            Self::ReqwestMessage(error) => write!(f, "Google Maps Geocoding API client in the Reqwest library: {error}"),
104            Self::SerdeJson(error) => write!(f, "Google Maps Geocoding API client in the Serde JSON library: {error}"),
105        } } } #[cfg(feature = "enable-reqwest")]
112impl From<reqwest::Error> for Error {
113    fn from(error: reqwest::Error) -> Self {
118        Self::Reqwest(crate::ReqError::from(error))
119    } } impl From<serde_json::error::Error> for Error {
125    fn from(error: serde_json::error::Error) -> Self {
130        Self::SerdeJson(error)
131    } }