Skip to content

Commit

Permalink
feat: [torrust#445] new custom error and minor refactor to extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-nt committed Feb 6, 2024
1 parent b5da547 commit 1cecc59
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ pub enum ServiceError {
#[display(fmt = "Database error.")]
DatabaseError,

#[display(fmt = "You must be logged in!.")]
LoggedInUserNotFound,

// Begin tracker errors
#[display(fmt = "Sorry, we have an error with our tracker connection.")]
TrackerOffline,
Expand Down Expand Up @@ -311,6 +314,7 @@ pub fn http_status_code_for_service_error(error: &ServiceError) -> StatusCode {
ServiceError::TrackerUnknownResponse => StatusCode::INTERNAL_SERVER_ERROR,
ServiceError::TorrentNotFoundInTracker => StatusCode::NOT_FOUND,
ServiceError::InvalidTrackerToken => StatusCode::INTERNAL_SERVER_ERROR,
ServiceError::LoggedInUserNotFound => StatusCode::UNAUTHORIZED,
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/web/api/server/v1/extractors/user_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ where
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
let maybe_bearer_token = match bearer_token::Extract::from_request_parts(parts, state).await {
Ok(maybe_bearer_token) => maybe_bearer_token.0,
Err(_) => return Err(ServiceError::Unauthorized.into_response()),
Err(_) => return Err(ServiceError::TokenNotFound.into_response()),
};

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

match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await {
Ok(user_id) => Ok(ExtractLoggedInUser(user_id)),
Err(_) => Err(ServiceError::Unauthorized.into_response()),
Err(_) => Err(ServiceError::LoggedInUserNotFound.into_response()),
}
}
}

0 comments on commit 1cecc59

Please sign in to comment.