Skip to content

Commit 53e5ef2

Browse files
committed
Extract error fallback_response behavior
Errors that implement their own `response` method do not need to impl any of the fallback methods. This change should help avoid adding new errors that rely on this fallback behavior.
1 parent 1f290c7 commit 53e5ef2

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

src/util/errors.rs

+26-16
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,14 @@ pub trait AppError: Send + fmt::Display + fmt::Debug + 'static {
2626
None
2727
}
2828

29-
fn response(&self) -> Option<Response> {
30-
if self.cargo_err() {
29+
/// Generate an HTTP response for the error
30+
fn response(&self) -> Option<Response>;
31+
32+
/// Fallback logic for generating a cargo friendly response
33+
///
34+
/// This behavior is deprecated and no new calls or impls should be added.
35+
fn fallback_response(&self) -> Option<Response> {
36+
if self.fallback_with_description_as_bad_200() {
3137
Some(json_response(&Bad {
3238
errors: vec![StringError {
3339
detail: self.description().to_string(),
@@ -37,7 +43,13 @@ pub trait AppError: Send + fmt::Display + fmt::Debug + 'static {
3743
self.cause().and_then(AppError::response)
3844
}
3945
}
40-
fn cargo_err(&self) -> bool {
46+
47+
/// Determines if the `fallback_response` method should send the description as a status 200
48+
/// error to cargo, or send the cause response (if applicable).
49+
///
50+
/// This is only to be used by the `fallback_response` method. If your error type impls
51+
/// `response`, then there is no need to impl this method.
52+
fn fallback_with_description_as_bad_200(&self) -> bool {
4153
false
4254
}
4355

@@ -75,8 +87,8 @@ impl AppError for Box<dyn AppError> {
7587
fn cause(&self) -> Option<&dyn AppError> {
7688
(**self).cause()
7789
}
78-
fn cargo_err(&self) -> bool {
79-
(**self).cargo_err()
90+
fn fallback_with_description_as_bad_200(&self) -> bool {
91+
(**self).fallback_with_description_as_bad_200()
8092
}
8193
fn response(&self) -> Option<Response> {
8294
(**self).response()
@@ -152,8 +164,8 @@ impl<E: AppError> AppError for ChainedError<E> {
152164
fn response(&self) -> Option<Response> {
153165
self.error.response()
154166
}
155-
fn cargo_err(&self) -> bool {
156-
self.error.cargo_err()
167+
fn fallback_with_description_as_bad_200(&self) -> bool {
168+
self.error.fallback_with_description_as_bad_200()
157169
}
158170
}
159171

@@ -170,6 +182,9 @@ impl<E: Error + Send + 'static> AppError for E {
170182
fn description(&self) -> &str {
171183
Error::description(self)
172184
}
185+
fn response(&self) -> Option<Response> {
186+
self.fallback_response()
187+
}
173188
}
174189

175190
impl<E: Error + Send + 'static> From<E> for Box<dyn AppError> {
@@ -205,7 +220,10 @@ impl AppError for ConcreteAppError {
205220
fn cause(&self) -> Option<&dyn AppError> {
206221
self.cause.as_ref().map(|c| &**c)
207222
}
208-
fn cargo_err(&self) -> bool {
223+
fn response(&self) -> Option<Response> {
224+
self.fallback_response()
225+
}
226+
fn fallback_with_description_as_bad_200(&self) -> bool {
209227
self.cargo_err
210228
}
211229
}
@@ -364,10 +382,6 @@ impl AppError for ReadOnlyMode {
364382
response.status = (503, "Service Unavailable");
365383
Some(response)
366384
}
367-
368-
fn cargo_err(&self) -> bool {
369-
true
370-
}
371385
}
372386

373387
impl fmt::Display for ReadOnlyMode {
@@ -406,10 +420,6 @@ impl AppError for TooManyRequests {
406420
.insert("Retry-After".into(), vec![retry_after.to_string()]);
407421
Some(response)
408422
}
409-
410-
fn cargo_err(&self) -> bool {
411-
true
412-
}
413423
}
414424

415425
impl fmt::Display for TooManyRequests {

0 commit comments

Comments
 (0)