Skip to content

Commit b35f59f

Browse files
committed
Add a not_found() helper for typical usage of NotFound
Some direct usage exists so the NotFound type is also re-exported.
1 parent f4ccc1b commit b35f59f

File tree

4 files changed

+34
-31
lines changed

4 files changed

+34
-31
lines changed

src/github.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use serde::de::DeserializeOwned;
88
use std::str;
99

1010
use crate::app::App;
11-
use crate::util::errors::{cargo_err, internal, AppError, AppResult, NotFound};
11+
use crate::util::errors::{cargo_err, internal, not_found, AppError, AppResult};
1212

1313
/// Does all the nonsense for sending a GET to Github. Doesn't handle parsing
1414
/// because custom error-code handling may be desirable. Use
@@ -46,7 +46,7 @@ fn handle_error_response(app: &App, error: &reqwest::Error) -> Box<dyn AppError>
4646
https://{}/login",
4747
app.config.domain_name,
4848
)),
49-
Some(Status::NOT_FOUND) => Box::new(NotFound),
49+
Some(Status::NOT_FOUND) => not_found(),
5050
_ => internal(&format_args!(
5151
"didn't get a 200 result from github: {}",
5252
error

src/router.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl Handler for R404 {
178178
req.mut_extensions().insert(m.params.clone());
179179
m.handler.call(req)
180180
}
181-
Err(..) => Ok(NotFound.response().unwrap()),
181+
Err(..) => Ok(NotFound.into()),
182182
}
183183
}
184184
}
@@ -187,7 +187,7 @@ impl Handler for R404 {
187187
mod tests {
188188
use super::*;
189189
use crate::util::errors::{
190-
bad_request, cargo_err, forbidden, internal, AppError, ChainError, NotFound,
190+
bad_request, cargo_err, forbidden, internal, not_found, AppError, ChainError,
191191
};
192192
use crate::util::EndpointResult;
193193

@@ -220,7 +220,7 @@ mod tests {
220220
StatusCode::NOT_FOUND
221221
);
222222
assert_eq!(
223-
C(|_| err(NotFound)).call(&mut req).unwrap().status(),
223+
C(|_| Err(not_found())).call(&mut req).unwrap().status(),
224224
StatusCode::NOT_FOUND
225225
);
226226

src/util/errors.rs

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ use crate::util::{json_response, AppResponse};
2525
pub(super) mod concrete;
2626
mod http;
2727

28+
pub(crate) use self::http::NotFound;
29+
2830
/// Returns an error with status 200 and the provided description as JSON
2931
///
3032
/// This is for backwards compatibility with cargo endpoints. For all other
@@ -48,6 +50,10 @@ pub fn forbidden() -> Box<dyn AppError> {
4850
Box::new(http::Forbidden)
4951
}
5052

53+
pub fn not_found() -> Box<dyn AppError> {
54+
Box::new(http::NotFound)
55+
}
56+
5157
/// Returns an error with status 500 and the provided description as JSON
5258
pub fn server_error<S: ToString + ?Sized>(error: &S) -> Box<dyn AppError> {
5359
Box::new(http::ServerError(error.to_string()))
@@ -103,7 +109,7 @@ impl dyn AppError {
103109

104110
fn try_convert(err: &(dyn Error + Send + 'static)) -> Option<Box<Self>> {
105111
match err.downcast_ref() {
106-
Some(DieselError::NotFound) => Some(Box::new(NotFound)),
112+
Some(DieselError::NotFound) => Some(not_found()),
107113
Some(DieselError::DatabaseError(_, info))
108114
if info.message().ends_with("read-only transaction") =>
109115
{
@@ -221,29 +227,6 @@ impl AppError for InternalAppError {
221227
}
222228
}
223229

224-
// TODO: The remaining can probably move under `http`
225-
226-
#[derive(Debug, Clone, Copy)]
227-
pub struct NotFound;
228-
229-
impl From<NotFound> for AppResponse {
230-
fn from(_: NotFound) -> AppResponse {
231-
json_error("Not Found", StatusCode::NOT_FOUND)
232-
}
233-
}
234-
235-
impl AppError for NotFound {
236-
fn response(&self) -> Option<AppResponse> {
237-
Some(NotFound.into())
238-
}
239-
}
240-
241-
impl fmt::Display for NotFound {
242-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
243-
"Not Found".fmt(f)
244-
}
245-
}
246-
247230
pub fn internal<S: ToString + ?Sized>(error: &S) -> Box<dyn AppError> {
248231
Box::new(InternalAppError {
249232
description: error.to_string(),

src/util/errors/http.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,29 @@ use conduit::StatusCode;
88
// The following structs are emtpy and do not provide a custom message to the user
99

1010
#[derive(Debug)]
11-
pub(super) struct Forbidden;
11+
pub(crate) struct NotFound;
12+
13+
// This struct has this helper impl for use as `NotFound.into()`
14+
impl From<NotFound> for AppResponse {
15+
fn from(_: NotFound) -> AppResponse {
16+
json_error("Not Found", StatusCode::NOT_FOUND)
17+
}
18+
}
19+
20+
impl AppError for NotFound {
21+
fn response(&self) -> Option<AppResponse> {
22+
Some(Self.into())
23+
}
24+
}
25+
26+
impl fmt::Display for NotFound {
27+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28+
"Not Found".fmt(f)
29+
}
30+
}
31+
1232
#[derive(Debug)]
13-
pub struct NotFound;
33+
pub(super) struct Forbidden;
1434

1535
impl AppError for Forbidden {
1636
fn response(&self) -> Option<AppResponse> {

0 commit comments

Comments
 (0)