@@ -42,7 +42,6 @@ struct Bad {
42
42
// AppError trait
43
43
44
44
pub trait AppError : Send + fmt:: Display + fmt:: Debug + ' static {
45
- fn description ( & self ) -> & str ;
46
45
fn cause ( & self ) -> Option < & ( dyn AppError ) > {
47
46
None
48
47
}
@@ -53,30 +52,6 @@ pub trait AppError: Send + fmt::Display + fmt::Debug + 'static {
53
52
/// where it is eventually logged and turned into a status 500 response.
54
53
fn response ( & self ) -> Option < Response > ;
55
54
56
- /// Fallback logic for generating a cargo friendly response
57
- ///
58
- /// This behavior is deprecated and no new calls or impls should be added.
59
- fn fallback_response ( & self ) -> Option < Response > {
60
- if self . fallback_with_description_as_bad_200 ( ) {
61
- Some ( json_response ( & Bad {
62
- errors : vec ! [ StringError {
63
- detail: self . description( ) . to_string( ) ,
64
- } ] ,
65
- } ) )
66
- } else {
67
- self . cause ( ) . and_then ( AppError :: response)
68
- }
69
- }
70
-
71
- /// Determines if the `fallback_response` method should send the description as a status 200
72
- /// error to cargo, or send the cause response (if applicable).
73
- ///
74
- /// This is only to be used by the `fallback_response` method. If your error type impls
75
- /// `response`, then there is no need to impl this method.
76
- fn fallback_with_description_as_bad_200 ( & self ) -> bool {
77
- false
78
- }
79
-
80
55
fn get_type_id ( & self ) -> TypeId {
81
56
TypeId :: of :: < Self > ( )
82
57
}
@@ -105,15 +80,9 @@ impl dyn AppError {
105
80
}
106
81
107
82
impl AppError for Box < dyn AppError > {
108
- fn description ( & self ) -> & str {
109
- ( * * self ) . description ( )
110
- }
111
83
fn cause ( & self ) -> Option < & dyn AppError > {
112
84
( * * self ) . cause ( )
113
85
}
114
- fn fallback_with_description_as_bad_200 ( & self ) -> bool {
115
- ( * * self ) . fallback_with_description_as_bad_200 ( )
116
- }
117
86
fn response ( & self ) -> Option < Response > {
118
87
( * * self ) . response ( )
119
88
}
@@ -179,18 +148,12 @@ impl<T> ChainError<T> for Option<T> {
179
148
}
180
149
181
150
impl < E : AppError > AppError for ChainedError < E > {
182
- fn description ( & self ) -> & str {
183
- self . error . description ( )
184
- }
185
151
fn cause ( & self ) -> Option < & dyn AppError > {
186
152
Some ( & * self . cause )
187
153
}
188
154
fn response ( & self ) -> Option < Response > {
189
155
self . error . response ( )
190
156
}
191
- fn fallback_with_description_as_bad_200 ( & self ) -> bool {
192
- self . error . fallback_with_description_as_bad_200 ( )
193
- }
194
157
}
195
158
196
159
impl < E : AppError > fmt:: Display for ChainedError < E > {
@@ -203,11 +166,8 @@ impl<E: AppError> fmt::Display for ChainedError<E> {
203
166
// Error impls
204
167
205
168
impl < E : Error + Send + ' static > AppError for E {
206
- fn description ( & self ) -> & str {
207
- Error :: description ( self )
208
- }
209
169
fn response ( & self ) -> Option < Response > {
210
- self . fallback_response ( )
170
+ None
211
171
}
212
172
}
213
173
@@ -219,6 +179,7 @@ impl<E: Error + Send + 'static> From<E> for Box<dyn AppError> {
219
179
// =============================================================================
220
180
// Concrete errors
221
181
182
+ // TODO: Rename to InternalAppError
222
183
#[ derive( Debug ) ]
223
184
struct ConcreteAppError {
224
185
description : String ,
@@ -232,25 +193,15 @@ impl fmt::Display for ConcreteAppError {
232
193
}
233
194
234
195
impl AppError for ConcreteAppError {
235
- fn description ( & self ) -> & str {
236
- & self . description
237
- }
238
- fn cause ( & self ) -> Option < & dyn AppError > {
239
- None
240
- }
241
196
fn response ( & self ) -> Option < Response > {
242
- self . fallback_response ( )
197
+ None
243
198
}
244
199
}
245
200
246
201
#[ derive( Debug , Clone , Copy ) ]
247
202
pub struct NotFound ;
248
203
249
204
impl AppError for NotFound {
250
- fn description ( & self ) -> & str {
251
- "not found"
252
- }
253
-
254
205
fn response ( & self ) -> Option < Response > {
255
206
let mut response = json_response ( & Bad {
256
207
errors : vec ! [ StringError {
@@ -272,10 +223,6 @@ impl fmt::Display for NotFound {
272
223
pub struct Unauthorized ;
273
224
274
225
impl AppError for Unauthorized {
275
- fn description ( & self ) -> & str {
276
- "unauthorized"
277
- }
278
-
279
226
fn response ( & self ) -> Option < Response > {
280
227
let mut response = json_response ( & Bad {
281
228
errors : vec ! [ StringError {
@@ -297,10 +244,6 @@ impl fmt::Display for Unauthorized {
297
244
struct BadRequest ( String ) ;
298
245
299
246
impl AppError for BadRequest {
300
- fn description ( & self ) -> & str {
301
- self . 0 . as_ref ( )
302
- }
303
-
304
247
fn response ( & self ) -> Option < Response > {
305
248
let mut response = json_response ( & Bad {
306
249
errors : vec ! [ StringError {
@@ -338,11 +281,7 @@ pub fn bad_request<S: ToString + ?Sized>(error: &S) -> Box<dyn AppError> {
338
281
#[ derive( Debug ) ]
339
282
pub struct AppErrToStdErr ( pub Box < dyn AppError > ) ;
340
283
341
- impl Error for AppErrToStdErr {
342
- fn description ( & self ) -> & str {
343
- self . 0 . description ( )
344
- }
345
- }
284
+ impl Error for AppErrToStdErr { }
346
285
347
286
impl fmt:: Display for AppErrToStdErr {
348
287
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
@@ -370,10 +309,6 @@ pub(crate) fn std_error_no_send(e: Box<dyn AppError>) -> Box<dyn Error> {
370
309
pub struct ReadOnlyMode ;
371
310
372
311
impl AppError for ReadOnlyMode {
373
- fn description ( & self ) -> & str {
374
- "tried to write in read only mode"
375
- }
376
-
377
312
fn response ( & self ) -> Option < Response > {
378
313
let mut response = json_response ( & Bad {
379
314
errors : vec ! [ StringError {
@@ -399,10 +334,6 @@ pub struct TooManyRequests {
399
334
}
400
335
401
336
impl AppError for TooManyRequests {
402
- fn description ( & self ) -> & str {
403
- "too many requests"
404
- }
405
-
406
337
fn response ( & self ) -> Option < Response > {
407
338
const HTTP_DATE_FORMAT : & str = "%a, %d %b %Y %H:%M:%S GMT" ;
408
339
let retry_after = self . retry_after . format ( HTTP_DATE_FORMAT ) ;
0 commit comments