Skip to content

Commit 1cecc59

Browse files
committed
feat: [#445] new custom error and minor refactor to extractor
1 parent b5da547 commit 1cecc59

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/errors.rs

+4
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ pub enum ServiceError {
148148
#[display(fmt = "Database error.")]
149149
DatabaseError,
150150

151+
#[display(fmt = "You must be logged in!.")]
152+
LoggedInUserNotFound,
153+
151154
// Begin tracker errors
152155
#[display(fmt = "Sorry, we have an error with our tracker connection.")]
153156
TrackerOffline,
@@ -311,6 +314,7 @@ pub fn http_status_code_for_service_error(error: &ServiceError) -> StatusCode {
311314
ServiceError::TrackerUnknownResponse => StatusCode::INTERNAL_SERVER_ERROR,
312315
ServiceError::TorrentNotFoundInTracker => StatusCode::NOT_FOUND,
313316
ServiceError::InvalidTrackerToken => StatusCode::INTERNAL_SERVER_ERROR,
317+
ServiceError::LoggedInUserNotFound => StatusCode::UNAUTHORIZED,
314318
}
315319
}
316320

src/web/api/server/v1/extractors/user_id.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ where
2323
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
2424
let maybe_bearer_token = match bearer_token::Extract::from_request_parts(parts, state).await {
2525
Ok(maybe_bearer_token) => maybe_bearer_token.0,
26-
Err(_) => return Err(ServiceError::Unauthorized.into_response()),
26+
Err(_) => return Err(ServiceError::TokenNotFound.into_response()),
2727
};
2828

2929
//Extracts the app state
3030
let app_data = Arc::from_ref(state);
3131

3232
match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await {
3333
Ok(user_id) => Ok(ExtractLoggedInUser(user_id)),
34-
Err(_) => Err(ServiceError::Unauthorized.into_response()),
34+
Err(_) => Err(ServiceError::LoggedInUserNotFound.into_response()),
3535
}
3636
}
3737
}

0 commit comments

Comments
 (0)