Skip to main content

google_maps/distance_matrix/
error.rs

1//! Distance Matrix API error types and error messages.
2
3// -----------------------------------------------------------------------------
4//
5/// An error produced by a Google Maps Distance Matrix API request.
6#[derive(Clone, Debug, thiserror::Error, miette::Diagnostic)]
7pub enum Error {
8    // -------------------------------------------------------------------------
9    // Client-side errors:
10    // -------------------------------------------------------------------------
11
12    // Validation errors:
13
14    /// An arrival time may only be specified in Transit travel mode.
15    #[error("`with_arrival_time` method may only be used when \
16        `with_travel_mode` is set to `TravelMode::Transit`. The travel mode has
17        been set to `{0}` and the arrival time has been set to `{1}`")]
18    #[diagnostic(
19        code(google_maps::distance_matrix::validate::arrival_time_is_for_transit_only),
20        url(docsrs),
21        help("try again either with a travel mode of `TravelMode::Transit` or \
22            no arrival time")
23    )]
24    ArrivalTimeIsForTransitOnly(String, String),
25
26    /// Either departure time or arrival time may be specified, but not both.
27    #[error("`with_departure_time` method cannot be used when \
28        `with_arrival_time` has been set. The arrival time has been set to \
29        `{0}` and the departure time has been set to `{1}`")]
30    #[diagnostic(
31        code(google_maps::distance_matrix::validate::either_departure_time_or_arrival_time),
32        url(docsrs),
33        help("try again either with no arrival time or no departure time")
34    )]
35    EitherDepartureTimeOrArrivalTime(String, String),
36
37    /// Transit mode may only be specified in Transit travel mode.
38    #[error("`with_transit_modes` method may only be used when \
39        `with_travel_mode` is set to `TravelMode::Transit`. The travel mode \
40        has been set to `{0}` and the transit modes have been set to `{1}`")]
41    #[diagnostic(
42        code(google_maps::distance_matrix::validate::transit_mode_is_for_transit_only),
43        url(docsrs),
44        help("try again either with a travel mode of `TravelMode::Transit` or no transit modes")
45    )]
46    TransitModeIsForTransitOnly(String, String),
47
48    /// Transit routing preference may only be specified in Transit travel mode.
49    #[error("`with_transit_route_preference` method may only be used when \
50        `with_travel_mode` is set to `TravelMode::Transit`. The travel mode \
51        has been set to `{0}` and the transit route preference has been set to \
52        `{1}`")]
53    #[diagnostic(
54        code(google_maps::distance_matrix::validate::transit_mode_is_for_transit_only),
55        url(docsrs),
56        help("try again either with a travel mode of `TravelMode::Transit` or no transit route preference")
57    )]
58    TransitRoutePreferenceIsForTransitOnly(String, String),
59
60    // Parse errors:
61
62    /// Invalid element status code.
63    ///
64    /// Valid codes are `OK`, `NOT_FOUND`, `ZERO_RESULTS`, and
65    /// `MAX_ROUTE_LENGTH_EXCEEDED`.
66    #[error("invalid element status: `{0}`")]
67    #[diagnostic(
68        code(google_maps::distance_matrix::parse::invalid_element_status_code),
69        url("https://developers.google.com/maps/documentation/distance-matrix/distance-matrix#DistanceMatrixStatus"),
70        help("valid codes are `OK`, `NOT_FOUND`, `ZERO_RESULTS`, and \
71            `MAX_ROUTE_LENGTH_EXCEEDED`")
72    )]
73    InvalidElementStatusCode(String),
74
75    /// Invalid status code.
76    ///
77    /// Valid codes are `INVALID_REQUEST`, `MAX_ELEMENTS_EXCEEDED`,
78    /// `MAX_DIMENSIONS_EXCEEDED`, `OVER_QUERY_LIMIT`, `OVER_QUERY_LIMIT`,
79    /// `REQUEST_DENIED`, and `UNKNOWN_ERROR`.
80    #[error("invalid status: `{0}`")]
81    #[diagnostic(
82        code(google_maps::distance_matrix::parse::invalid_status_code),
83        url("https://developers.google.com/maps/documentation/directions/get-directions#DirectionsStatus"),
84        help("valid codes are `INVALID_REQUEST`, `MAX_ELEMENTS_EXCEEDED`, \
85            `MAX_DIMENSIONS_EXCEEDED`, `OVER_QUERY_LIMIT`, `OVER_QUERY_LIMIT`, \
86            `REQUEST_DENIED`, and `UNKNOWN_ERROR`")
87    )]
88    InvalidStatusCode(String),
89
90    // -------------------------------------------------------------------------
91    // Server-side errors (statuses):
92    // -------------------------------------------------------------------------
93
94    /// Invalid request indicates that the provided request was invalid. Common
95    /// causes of this status include an invalid parameter or parameter value.
96    #[error("invalid request")]
97    #[diagnostic(
98        code(google_maps::distance_matrix::status::invalid_request),
99        url("https://developers.google.com/maps/documentation/distance-matrix/distance-matrix#DistanceMatrixStatus"),
100        help("indicates that the provided request was invalid. Common causes \
101            of this status include an invalid parameter or parameter value")
102    )]
103    InvalidRequest,
104
105    /// Maximum elements exceeded indicates that the product of origins and
106    /// destinations exceeds the per-query limit.
107    #[error("maximum elements exceeded")]
108    #[diagnostic(
109        code(google_maps::distance_matrix::status::max_elements_exceeded),
110        url("https://developers.google.com/maps/documentation/distance-matrix/distance-matrix#DistanceMatrixStatus"),
111        help("indicates that the product of origins and destinations exceeds \
112            the per-query limit")
113    )]
114    MaxElementsExceeded,
115
116    /// Maximum dimensions exceeded indicates that the number of origins or
117    /// destinations exceeds the per-query limit.
118    #[error("maximum dimensions exceeded")]
119    #[diagnostic(
120        code(google_maps::distance_matrix::status::max_dimensions_exceeded),
121        url("https://developers.google.com/maps/documentation/distance-matrix/distance-matrix#DistanceMatrixStatus"),
122        help("indicates that the number of origins or destinations exceeds the \
123            per-query limit")
124    )]
125    MaxDimensionsExceeded,
126
127    /// Over daily limit indicates that the request was denied for one or more
128    /// of the following reasons:
129    ///
130    /// * The API key is missing or invalid.
131    ///
132    /// * Billing has not been enabled on your account.
133    ///
134    /// * A self-imposed usage cap has been exceeded.
135    ///
136    /// * The provided method of payment is no longer valid (for example, a
137    ///   credit card has expired).
138    ///
139    /// In order to use Google Maps Platform products, billing must be enabled
140    /// on your account, and all requests must include a valid API key. To fix
141    /// this, take the following steps:
142    ///
143    /// * [Get an API key](https://developers.google.com/maps/documentation/roads/errors?hl=en#new-key)
144    ///
145    /// * [Enable billing](https://console.cloud.google.com/project/_/billing/enable)
146    ///   on your account.
147    ///
148    /// * [Adjust your usage cap](https://developers.google.com/maps/documentation/roads/errors?hl=en#usage-cap)
149    ///   to increase your daily limit (if applicable).
150    #[error("over daily limit")]
151    #[diagnostic(
152        code(google_maps::distance_matrix::status::over_daily_limit),
153        url("https://developers.google.com/maps/faq#over-limit-key-error"),
154        help("either the API key is missing or invalid, billing has not been \
155            enabled on your account, a self-imposed usage cap has been \
156            exceeded, or the provided method of payment is no longer valid \
157            (for example, a credit card has expired)")
158    )]
159    OverDailyLimit,
160
161    /// Overy query limit indicates any of the following:
162    ///
163    /// * You have exceeded the QPS limits.
164    ///
165    /// * Billing has not been enabled on your account.
166    ///
167    /// * The monthly $200 credit, or a self-imposed usage cap, has been
168    ///   exceeded.
169    ///
170    /// * The provided method of payment is no longer valid (for example, a
171    ///   credit card has expired).
172    ///
173    /// See the [Maps FAQ](https://developers.google.com/maps/faq#over-limit-key-error)
174    /// for more information about how to resolve this error.
175    #[error("over query limit")]
176    #[diagnostic(
177        code(google_maps::distance_matrix::status::over_query_limit),
178        url("https://developers.google.com/maps/faq#over-limit-key-error"),
179        help("either you have exceeded the QPS limits, billing has not been \
180            enabled on your account, a self-imposed usage cap has been \
181            exceeded or the provided method of payment is no longer valid")
182    )]
183    OverQueryLimit,
184
185    /// Request denied by the server.
186    #[error("request denied")]
187    #[diagnostic(
188        code(google_maps::distance_matrix::status::request_denied),
189        url("https://developers.google.com/maps/documentation/directions/get-directions#DirectionsStatus"),
190        help("indicates that the service denied use of the Distance Matrix \
191            service by your application")
192    )]
193    RequestDenied,
194
195    /// Unknown error from the server.
196    #[error("unknown error")]
197    #[diagnostic(
198        code(google_maps::distance_matrix::status::unknown_error),
199        url("https://developers.google.com/maps/documentation/distance-matrix/distance-matrix#DistanceMatrixStatus"),
200        help("indicates a Distance Matrix request could not be processed due \
201            to a server error. The request may succeed if you try again.")
202    )]
203    UnknownError,
204} // enum Error
205
206// -----------------------------------------------------------------------------
207
208use crate::ClassifiedError;
209
210impl crate::traits::ClassifiableError<'_, Self> for Error {
211    /// Classifies an API error as a `Transient` error or `Permanent` error.
212    ///
213    /// This classification will, in turn, be used to decide whether the HTTP
214    /// request should be retried or not.
215    fn classify(&self) -> ClassifiedError<'_, Self> {
216        match self {
217            Self::UnknownError => ClassifiedError::Transient(self),
218            _ => ClassifiedError::Permanent(self),
219        } // match
220    } // fn
221} // impl