Skip to content

Commit 3e7fb4f

Browse files
committed
refactor: added error handling for unsafe unwraps
1 parent a9ddb94 commit 3e7fb4f

File tree

4 files changed

+23
-26
lines changed

4 files changed

+23
-26
lines changed

src/services/proxy.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ impl Service {
4343
///
4444
/// The function panics if the optional user id has no value
4545
pub async fn get_image_by_url(&self, url: &str, maybe_user_id: Option<UserId>) -> Result<Bytes, Error> {
46+
let Some(user_id) = maybe_user_id else {
47+
return Err(Error::Unauthenticated);
48+
};
49+
4650
self.authorization_service
4751
.authorize(ACTION::GetImageByUrl, maybe_user_id)
4852
.await
4953
.map_err(|_| Error::Unauthenticated)?;
5054

51-
self.image_cache_service
52-
.get_image_by_url(url, maybe_user_id.expect("There is no user id needed to perform the action"))
53-
.await
55+
self.image_cache_service.get_image_by_url(url, user_id).await
5456
}
5557
}

src/services/torrent.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ impl Index {
135135
add_torrent_req: AddTorrentRequest,
136136
maybe_user_id: Option<UserId>,
137137
) -> Result<AddTorrentResponse, ServiceError> {
138+
let Some(user_id) = maybe_user_id else {
139+
return Err(ServiceError::UnauthorizedActionForGuests);
140+
};
141+
138142
self.authorization_service
139143
.authorize(ACTION::AddTorrent, maybe_user_id)
140144
.await?;
@@ -150,12 +154,7 @@ impl Index {
150154

151155
let torrent_id = self
152156
.torrent_repository
153-
.add(
154-
&original_info_hash,
155-
&torrent,
156-
&metadata,
157-
maybe_user_id.expect("There is no user id needed to perform the action"),
158-
)
157+
.add(&original_info_hash, &torrent, &metadata, user_id)
159158
.await?;
160159

161160
// Synchronous secondary tasks

src/services/user.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -235,20 +235,21 @@ impl ProfileService {
235235
maybe_user_id: Option<UserId>,
236236
change_password_form: &ChangePasswordForm,
237237
) -> Result<(), ServiceError> {
238+
let Some(user_id) = maybe_user_id else {
239+
return Err(ServiceError::UnauthorizedActionForGuests);
240+
};
241+
238242
self.authorization_service
239243
.authorize(ACTION::ChangePassword, maybe_user_id)
240244
.await?;
241245

242-
info!(
243-
"changing user password for user ID: {}",
244-
maybe_user_id.expect("There is no user id needed to perform the action")
245-
);
246+
info!("changing user password for user ID: {}", user_id);
246247

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

249250
let user_authentication = self
250251
.user_authentication_repository
251-
.get_user_authentication_from_id(&maybe_user_id.expect("There is no user id needed to perform the action"))
252+
.get_user_authentication_from_id(&user_id)
252253
.await?;
253254

254255
verify_password(change_password_form.current_password.as_bytes(), &user_authentication)?;
@@ -267,10 +268,7 @@ impl ProfileService {
267268
let password_hash = hash_password(&change_password_form.password)?;
268269

269270
self.user_authentication_repository
270-
.change_password(
271-
maybe_user_id.expect("There is no user id needed to perform the action"),
272-
&password_hash,
273-
)
271+
.change_password(user_id, &password_hash)
274272
.await?;
275273

276274
Ok(())
@@ -310,13 +308,14 @@ impl BanService {
310308
///
311309
/// The function panics if the optional user id has no value
312310
pub async fn ban_user(&self, username_to_be_banned: &str, maybe_user_id: Option<UserId>) -> Result<(), ServiceError> {
313-
debug!(
314-
"user with ID {} banning username: {username_to_be_banned}",
315-
maybe_user_id.expect("There is no user id needed to perform the action")
316-
);
311+
let Some(user_id) = maybe_user_id else {
312+
return Err(ServiceError::UnauthorizedActionForGuests);
313+
};
317314

318315
self.authorization_service.authorize(ACTION::BanUser, maybe_user_id).await?;
319316

317+
debug!("user with ID {} banning username: {username_to_be_banned}", user_id);
318+
320319
let user_profile = self
321320
.user_profile_repository
322321
.get_user_profile_from_username(username_to_be_banned)

src/web/api/server/v1/contexts/user/handlers.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,7 @@ pub async fn change_password_handler(
145145
.await
146146
{
147147
Ok(()) => Json(OkResponseData {
148-
data: format!(
149-
"Password changed for user with ID: {}",
150-
maybe_user_id.expect("There is no user id needed to perform the action")
151-
),
148+
data: format!("Password changed for user with ID: {}", maybe_user_id.unwrap()),
152149
})
153150
.into_response(),
154151
Err(error) => error.into_response(),

0 commit comments

Comments
 (0)