Skip to content

Commit

Permalink
refactor: added error messages and panics section
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-nt committed Aug 13, 2024
1 parent 86c0fd5 commit a9ddb94
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
9 changes: 7 additions & 2 deletions src/services/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,18 @@ impl Service {
/// * The image URL is not an image.
/// * The image is too big.
/// * The user quota is met.
///
/// # Panics
///
/// The function panics if the optional user id has no value
pub async fn get_image_by_url(&self, url: &str, maybe_user_id: Option<UserId>) -> Result<Bytes, Error> {
self.authorization_service
.authorize(ACTION::GetImageByUrl, maybe_user_id)
.await
.map_err(|_| Error::Unauthenticated)?;

// The unwrap should never panic as if the maybe_user_id is none, an authorization error will be returned and handled at the method above
self.image_cache_service.get_image_by_url(url, maybe_user_id.unwrap()).await
self.image_cache_service
.get_image_by_url(url, maybe_user_id.expect("There is no user id needed to perform the action"))
.await
}
}
8 changes: 7 additions & 1 deletion src/services/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ impl Index {
/// This function will panic if:
///
/// * Unable to parse the torrent info-hash.
/// * The optional user id has no value
pub async fn add_torrent(
&self,
add_torrent_req: AddTorrentRequest,
Expand All @@ -149,7 +150,12 @@ impl Index {

let torrent_id = self
.torrent_repository
.add(&original_info_hash, &torrent, &metadata, maybe_user_id.unwrap())
.add(
&original_info_hash,
&torrent,
&metadata,
maybe_user_id.expect("There is no user id needed to perform the action"),
)
.await?;

// Synchronous secondary tasks
Expand Down
20 changes: 16 additions & 4 deletions src/services/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ impl ProfileService {
/// * An error if unable to successfully hash the password.
/// * An error if unable to change the password in the database.
/// * An error if it is not possible to authorize the action
/// # Panics
///
/// The function panics if the optional user id has no value
pub async fn change_password(
&self,
maybe_user_id: Option<UserId>,
Expand All @@ -236,13 +239,16 @@ impl ProfileService {
.authorize(ACTION::ChangePassword, maybe_user_id)
.await?;

info!("changing user password for user ID: {}", maybe_user_id.unwrap());
info!(
"changing user password for user ID: {}",
maybe_user_id.expect("There is no user id needed to perform the action")
);

let settings = self.configuration.settings.read().await;

let user_authentication = self
.user_authentication_repository
.get_user_authentication_from_id(&maybe_user_id.unwrap())
.get_user_authentication_from_id(&maybe_user_id.expect("There is no user id needed to perform the action"))
.await?;

verify_password(change_password_form.current_password.as_bytes(), &user_authentication)?;
Expand All @@ -261,7 +267,10 @@ impl ProfileService {
let password_hash = hash_password(&change_password_form.password)?;

self.user_authentication_repository
.change_password(maybe_user_id.unwrap(), &password_hash)
.change_password(
maybe_user_id.expect("There is no user id needed to perform the action"),
&password_hash,
)
.await?;

Ok(())
Expand Down Expand Up @@ -297,10 +306,13 @@ impl BanService {
/// * `ServiceError::InternalServerError` if unable get user from the request.
/// * An error if unable to get user profile from supplied username.
/// * An error if unable to set the ban of the user in the database.
/// # Panics
///
/// The function panics if the optional user id has no value
pub async fn ban_user(&self, username_to_be_banned: &str, maybe_user_id: Option<UserId>) -> Result<(), ServiceError> {
debug!(
"user with ID {} banning username: {username_to_be_banned}",
maybe_user_id.unwrap()
maybe_user_id.expect("There is no user id needed to perform the action")
);

self.authorization_service.authorize(ACTION::BanUser, maybe_user_id).await?;
Expand Down
8 changes: 7 additions & 1 deletion src/web/api/server/v1/contexts/user/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ pub async fn renew_token_handler(
/// It returns an error if:
///
/// - The user account is not found.
/// # Panics
///
/// The function panics if the optional user id has no value
#[allow(clippy::unused_async)]
pub async fn change_password_handler(
State(app_data): State<Arc<AppData>>,
Expand All @@ -142,7 +145,10 @@ pub async fn change_password_handler(
.await
{
Ok(()) => Json(OkResponseData {
data: format!("Password changed for user with ID: {}", maybe_user_id.unwrap()),
data: format!(
"Password changed for user with ID: {}",
maybe_user_id.expect("There is no user id needed to perform the action")
),
})
.into_response(),
Err(error) => error.into_response(),
Expand Down

0 comments on commit a9ddb94

Please sign in to comment.