Skip to content

Commit 195f603

Browse files
committed
Simplify implementation of cargo_err
1 parent cd6c3fe commit 195f603

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/util/errors.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ mod http;
1616
/// endpoints, use helpers like `bad_request` or `server_error` which set a
1717
/// correct status code.
1818
pub fn cargo_err<S: ToString + ?Sized>(error: &S) -> Box<dyn AppError> {
19-
Box::new(ConcreteAppError {
20-
description: error.to_string(),
21-
cargo_err: true,
22-
})
19+
Box::new(http::Ok(error.to_string()))
2320
}
2421

2522
// The following are intended to be used for errors being sent back to the Ember
@@ -51,6 +48,9 @@ pub trait AppError: Send + fmt::Display + fmt::Debug + 'static {
5148
}
5249

5350
/// Generate an HTTP response for the error
51+
///
52+
/// If none is returned, the error will bubble up the middleware stack
53+
/// where it is eventually logged and turned into a status 500 response.
5454
fn response(&self) -> Option<Response>;
5555

5656
/// Fallback logic for generating a cargo friendly response
@@ -222,7 +222,6 @@ impl<E: Error + Send + 'static> From<E> for Box<dyn AppError> {
222222
#[derive(Debug)]
223223
struct ConcreteAppError {
224224
description: String,
225-
cargo_err: bool,
226225
}
227226

228227
impl fmt::Display for ConcreteAppError {
@@ -242,9 +241,6 @@ impl AppError for ConcreteAppError {
242241
fn response(&self) -> Option<Response> {
243242
self.fallback_response()
244243
}
245-
fn fallback_with_description_as_bad_200(&self) -> bool {
246-
self.cargo_err
247-
}
248244
}
249245

250246
#[derive(Debug, Clone, Copy)]
@@ -325,7 +321,6 @@ impl fmt::Display for BadRequest {
325321
pub fn internal<S: ToString + ?Sized>(error: &S) -> Box<dyn AppError> {
326322
Box::new(ConcreteAppError {
327323
description: error.to_string(),
328-
cargo_err: false,
329324
})
330325
}
331326

src/util/errors/http.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,31 @@ use conduit::Response;
55
use super::{AppError, Bad, StringError};
66
use crate::util::json_response;
77

8+
#[derive(Debug)]
9+
pub(super) struct Ok(pub(super) String);
810
#[derive(Debug)]
911
pub(super) struct ServerError(pub(super) String);
1012

13+
impl AppError for Ok {
14+
fn description(&self) -> &str {
15+
self.0.as_ref()
16+
}
17+
18+
fn response(&self) -> Option<Response> {
19+
Some(json_response(&Bad {
20+
errors: vec![StringError {
21+
detail: self.0.clone(),
22+
}],
23+
}))
24+
}
25+
}
26+
27+
impl fmt::Display for Ok {
28+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29+
self.0.fmt(f)
30+
}
31+
}
32+
1133
impl AppError for ServerError {
1234
fn description(&self) -> &str {
1335
self.0.as_ref()

0 commit comments

Comments
 (0)