@@ -26,8 +26,14 @@ pub trait AppError: Send + fmt::Display + fmt::Debug + 'static {
26
26
None
27
27
}
28
28
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 ( ) {
31
37
Some ( json_response ( & Bad {
32
38
errors : vec ! [ StringError {
33
39
detail: self . description( ) . to_string( ) ,
@@ -37,7 +43,13 @@ pub trait AppError: Send + fmt::Display + fmt::Debug + 'static {
37
43
self . cause ( ) . and_then ( AppError :: response)
38
44
}
39
45
}
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 {
41
53
false
42
54
}
43
55
@@ -75,8 +87,8 @@ impl AppError for Box<dyn AppError> {
75
87
fn cause ( & self ) -> Option < & dyn AppError > {
76
88
( * * self ) . cause ( )
77
89
}
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 ( )
80
92
}
81
93
fn response ( & self ) -> Option < Response > {
82
94
( * * self ) . response ( )
@@ -152,8 +164,8 @@ impl<E: AppError> AppError for ChainedError<E> {
152
164
fn response ( & self ) -> Option < Response > {
153
165
self . error . response ( )
154
166
}
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 ( )
157
169
}
158
170
}
159
171
@@ -170,6 +182,9 @@ impl<E: Error + Send + 'static> AppError for E {
170
182
fn description ( & self ) -> & str {
171
183
Error :: description ( self )
172
184
}
185
+ fn response ( & self ) -> Option < Response > {
186
+ self . fallback_response ( )
187
+ }
173
188
}
174
189
175
190
impl < E : Error + Send + ' static > From < E > for Box < dyn AppError > {
@@ -205,7 +220,10 @@ impl AppError for ConcreteAppError {
205
220
fn cause ( & self ) -> Option < & dyn AppError > {
206
221
self . cause . as_ref ( ) . map ( |c| & * * c)
207
222
}
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 {
209
227
self . cargo_err
210
228
}
211
229
}
@@ -364,10 +382,6 @@ impl AppError for ReadOnlyMode {
364
382
response. status = ( 503 , "Service Unavailable" ) ;
365
383
Some ( response)
366
384
}
367
-
368
- fn cargo_err ( & self ) -> bool {
369
- true
370
- }
371
385
}
372
386
373
387
impl fmt:: Display for ReadOnlyMode {
@@ -406,10 +420,6 @@ impl AppError for TooManyRequests {
406
420
. insert ( "Retry-After" . into ( ) , vec ! [ retry_after. to_string( ) ] ) ;
407
421
Some ( response)
408
422
}
409
-
410
- fn cargo_err ( & self ) -> bool {
411
- true
412
- }
413
423
}
414
424
415
425
impl fmt:: Display for TooManyRequests {
0 commit comments