Skip to content

Commit c317afb

Browse files
committed
Rename error struct to match its behavior
The error types maps to status `403 Forbidden`, not `400 Unauthorized`.
1 parent 2a77554 commit c317afb

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

src/controllers/util.rs

Lines changed: 4 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, Unauthorized};
5+
use crate::util::errors::{internal, AppError, AppResult, ChainError, Forbidden};
66

77
#[derive(Debug)]
88
pub struct AuthenticatedUser {
@@ -26,7 +26,7 @@ impl AuthenticatedUser {
2626
}
2727

2828
impl<'a> UserAuthenticationExt for dyn RequestExt + 'a {
29-
/// Obtain `AuthenticatedUser` for the request or return an `Unauthorized` error
29+
/// Obtain `AuthenticatedUser` for the request or return an `Forbidden` error
3030
fn authenticate(&self, conn: &PgConnection) -> AppResult<AuthenticatedUser> {
3131
if let Some(id) = self.extensions().find::<TrustedUserId>() {
3232
// A trusted user_id was provided by a signed cookie (or a test `MockCookieUser`)
@@ -43,11 +43,11 @@ 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(Unauthorized) as Box<dyn AppError>)
46+
.chain_error(|| Box::new(Forbidden) as Box<dyn AppError>)
4747
} else {
4848
// Unable to authenticate the user
4949
Err(internal("no cookie session or auth header found"))
50-
.chain_error(|| Box::new(Unauthorized) as Box<dyn AppError>)
50+
.chain_error(|| Box::new(Forbidden) as Box<dyn AppError>)
5151
}
5252
}
5353
}

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, NotFound, Unauthorized,
190+
bad_request, cargo_err, internal, AppError, ChainError, Forbidden, 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(Unauthorized)).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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,16 +241,16 @@ impl fmt::Display for NotFound {
241241
}
242242

243243
#[derive(Debug, Clone, Copy)]
244-
pub struct Unauthorized;
244+
pub struct Forbidden;
245245

246-
impl AppError for Unauthorized {
246+
impl AppError for Forbidden {
247247
fn response(&self) -> Option<AppResponse> {
248248
let detail = "must be logged in to perform that action";
249249
Some(json_error(detail, StatusCode::FORBIDDEN))
250250
}
251251
}
252252

253-
impl fmt::Display for Unauthorized {
253+
impl fmt::Display for Forbidden {
254254
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
255255
"must be logged in to perform that action".fmt(f)
256256
}
@@ -358,7 +358,7 @@ fn chain_error_internal() {
358358
"outer caused by inner"
359359
);
360360
assert_eq!(
361-
Err::<(), _>(Unauthorized)
361+
Err::<(), _>(Forbidden)
362362
.chain_error(|| internal("outer"))
363363
.unwrap_err()
364364
.to_string(),

0 commit comments

Comments
 (0)