use crate::roads::status::Status;
use miette::Diagnostic;
use thiserror::Error;
#[derive(Debug, Diagnostic, Error)]
#[diagnostic(code(google_maps::roads::error), url(docsrs))]
pub enum Error {
GoogleMapsService(Status, Option<String>),
HttpUnsuccessful(String),
InvalidStatusCode(String),
QueryNotBuilt,
#[cfg(feature = "enable-reqwest")]
Reqwest(crate::ReqError),
#[cfg(feature = "enable-reqwest")]
ReqwestMessage(String),
SerdeJson(serde_json::error::Error),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::GoogleMapsService(status, error_message) => match error_message {
Some(error_message) => write!(f, "Google Maps Roads API service: {error_message}"),
None => match status {
Status::InvalidArgument => write!(f, "Google Maps Roads API service: \
Invalid argument. \
1. Your API key is not valid or was not included in the request. \
or, 2. Your request contained invalid arguments."),
Status::PermissionDenied => write!(f, "Google Maps Roads API service: \
Permission Denied. \
1. API key missing or invalid. \
2. Billing not enabled. \
3. Self-imposed usage cap exceeded. \
or, 4. Method of payment no longer valid."),
Status::NotFound => write!(f, "Google Maps Roads API service: \
Not found. \
Ensure that you are sending requests \
to `https://roads.googleapis.com/` and not \
`http://roads.googleapis.com/`."),
Status::ResourceExhausted => write!(f, "Google Maps Roads API service: \
Not found. \
You have exceeded the request limit that you configured \
in the Google Cloud Platform Console."),
} }, Self::HttpUnsuccessful(status) => write!(f,
"Google Maps Roads API client: \
Could not successfully query the Google Cloud Platform service. \
The service last responded with a `{status}` status."),
Self::InvalidStatusCode(status_code) => write!(f, "Google Maps Roads API client: \
`{status_code}` is not a valid status code. \
Valid codes are `INVALID_ARGUMENT`, `PERMISSION_DENIED`, \
`NOT_FOUND`, and `RESOURCE_EXHAUSTED`."),
Self::QueryNotBuilt => write!(f, "Google Maps Roads API client library: \
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()."),
#[cfg(feature = "enable-reqwest")]
Self::Reqwest(error) => write!(f, "Google Maps Roads API client in the Reqwest library: {error}"),
#[cfg(feature = "enable-reqwest")]
Self::ReqwestMessage(error) => write!(f, "Google Maps Geocoding API client in the Reqwest library: {error}"),
Self::SerdeJson(error) => write!(f, "Google Maps Roads API client in the Serde JSON library: {error}"),
} } }
#[cfg(feature = "enable-reqwest")]
impl From<reqwest::Error> for Error {
fn from(error: reqwest::Error) -> Self {
Self::Reqwest(crate::ReqError::from(error))
} }
impl From<serde_json::error::Error> for Error {
fn from(error: serde_json::error::Error) -> Self {
Self::SerdeJson(error)
} }