use crate::elevation::response::status::Status;
use miette::Diagnostic;
use thiserror::Error;
#[derive(Debug, Diagnostic, Error)]
#[diagnostic(code(google_maps::elevation::error), url(docsrs))]
pub enum Error {
EitherPositionalOrSampledPath,
GoogleMapsService(Status, Option<String>),
HttpUnsuccessful(String),
InvalidStatusCode(String),
QueryNotBuilt,
RequestNotValidated,
#[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::EitherPositionalOrSampledPath => write!(f,
"Google Maps Elevation API client: \
A for_sampled_path_request() method cannot be used when for_postional_request() has been set. \
Try again with only a positional request or only a sampled path request."),
Self::GoogleMapsService(status, error_message) => match error_message {
Some(error_message) => write!(f, "Google Maps Elevation API service: {error_message}"),
None => match status {
Status::InvalidRequest => write!(f, "Google Maps Elevation API service: \
Invalid request. \
The request was malformed."),
Status::Ok => write!(f, "Google Maps Elevation service: \
Ok. \
The request was successful."),
Status::OverDailyLimit => write!(f, "Google Maps Elevation API service: \
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 Elevation API service: \
Over query limit. \
Requestor has exceeded quota."),
Status::RequestDenied => write!(f, "Google Maps Elevation API service: \
Request denied \
Service did not complete the request."),
Status::UnknownError => write!(f, "Google Maps Elevation API service: \
Unknown error."),
} }, Self::HttpUnsuccessful(status) => write!(f,
"Google Maps Elevation 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 Elevation API client: \
`{status_code}` is not a valid status code. \
Valid codes are `INVALID_REQUEST`, `OK`, `OVER_DAILY_LIMIT`, \
`OVER_QUERY_LIMIT`, `REQUEST_DENIED`, and `UNKNOWN_ERROR`."
),
Self::RequestNotValidated => write!(f,
"Google Maps Elevation API client: \
The request must be validated before a query string may be built. \
Ensure the validate() method is called before build()."),
#[cfg(feature = "enable-reqwest")]
Self::Reqwest(error) => write!(f, "Google Maps Elevation 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 Elevation API client in the Serde JSON library: {error}"),
Self::QueryNotBuilt => write!(f,
"Google Maps Elevation 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()."),
} } }
#[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)
} }