Skip to content

Commit f4ccc1b

Browse files
committed
Move forbidden into errors::http
1 parent c317afb commit f4ccc1b

File tree

4 files changed

+32
-23
lines changed

4 files changed

+32
-23
lines changed

src/controllers/util.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::prelude::*;
22

33
use crate::middleware::current_user::TrustedUserId;
44
use crate::models::{ApiToken, User};
5-
use crate::util::errors::{internal, AppError, AppResult, ChainError, Forbidden};
5+
use crate::util::errors::{forbidden, internal, AppResult, ChainError};
66

77
#[derive(Debug)]
88
pub struct AuthenticatedUser {
@@ -43,11 +43,10 @@ impl<'a> UserAuthenticationExt for dyn RequestExt + 'a {
4343
token_id: Some(token.id),
4444
})
4545
.chain_error(|| internal("invalid token"))
46-
.chain_error(|| Box::new(Forbidden) as Box<dyn AppError>)
46+
.chain_error(forbidden)
4747
} else {
4848
// Unable to authenticate the user
49-
Err(internal("no cookie session or auth header found"))
50-
.chain_error(|| Box::new(Forbidden) as Box<dyn AppError>)
49+
Err(internal("no cookie session or auth header found")).chain_error(forbidden)
5150
}
5251
}
5352
}

src/router.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl Handler for R404 {
187187
mod tests {
188188
use super::*;
189189
use crate::util::errors::{
190-
bad_request, cargo_err, internal, AppError, ChainError, Forbidden, NotFound,
190+
bad_request, cargo_err, forbidden, internal, AppError, ChainError, NotFound,
191191
};
192192
use crate::util::EndpointResult;
193193

@@ -209,7 +209,7 @@ mod tests {
209209
StatusCode::BAD_REQUEST
210210
);
211211
assert_eq!(
212-
C(|_| err(Forbidden)).call(&mut req).unwrap().status(),
212+
C(|_| Err(forbidden())).call(&mut req).unwrap().status(),
213213
StatusCode::FORBIDDEN
214214
);
215215
assert_eq!(

src/util/errors.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ pub fn bad_request<S: ToString + ?Sized>(error: &S) -> Box<dyn AppError> {
4444
Box::new(http::BadRequest(error.to_string()))
4545
}
4646

47+
pub fn forbidden() -> Box<dyn AppError> {
48+
Box::new(http::Forbidden)
49+
}
50+
4751
/// Returns an error with status 500 and the provided description as JSON
4852
pub fn server_error<S: ToString + ?Sized>(error: &S) -> Box<dyn AppError> {
4953
Box::new(http::ServerError(error.to_string()))
@@ -240,22 +244,6 @@ impl fmt::Display for NotFound {
240244
}
241245
}
242246

243-
#[derive(Debug, Clone, Copy)]
244-
pub struct Forbidden;
245-
246-
impl AppError for Forbidden {
247-
fn response(&self) -> Option<AppResponse> {
248-
let detail = "must be logged in to perform that action";
249-
Some(json_error(detail, StatusCode::FORBIDDEN))
250-
}
251-
}
252-
253-
impl fmt::Display for Forbidden {
254-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
255-
"must be logged in to perform that action".fmt(f)
256-
}
257-
}
258-
259247
pub fn internal<S: ToString + ?Sized>(error: &S) -> Box<dyn AppError> {
260248
Box::new(InternalAppError {
261249
description: error.to_string(),
@@ -358,7 +346,7 @@ fn chain_error_internal() {
358346
"outer caused by inner"
359347
);
360348
assert_eq!(
361-
Err::<(), _>(Forbidden)
349+
Err::<(), _>(forbidden())
362350
.chain_error(|| internal("outer"))
363351
.unwrap_err()
364352
.to_string(),

src/util/errors/http.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,28 @@ use crate::util::AppResponse;
55

66
use conduit::StatusCode;
77

8+
// The following structs are emtpy and do not provide a custom message to the user
9+
10+
#[derive(Debug)]
11+
pub(super) struct Forbidden;
12+
#[derive(Debug)]
13+
pub struct NotFound;
14+
15+
impl AppError for Forbidden {
16+
fn response(&self) -> Option<AppResponse> {
17+
let detail = "must be logged in to perform that action";
18+
Some(json_error(detail, StatusCode::FORBIDDEN))
19+
}
20+
}
21+
22+
impl fmt::Display for Forbidden {
23+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24+
"must be logged in to perform that action".fmt(f)
25+
}
26+
}
27+
28+
// The following structs wrap a String and provide a custom message to the user
29+
830
#[derive(Debug)]
931
pub(super) struct Ok(pub(super) String);
1032
#[derive(Debug)]

0 commit comments

Comments
 (0)