Skip to content

Commit

Permalink
refactor: [#615] renamed error to be more human friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-nt committed Aug 3, 2024
1 parent 836c94d commit 42fb031
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub enum ServiceError {
InvalidTag,

#[display(fmt = "Unauthorized action.")]
Unauthorized,
UnauthorizedAction,

#[display(fmt = "This torrent already exists in our database.")]
InfoHashAlreadyExists,
Expand Down Expand Up @@ -300,7 +300,7 @@ pub fn http_status_code_for_service_error(error: &ServiceError) -> StatusCode {
ServiceError::MissingMandatoryMetadataFields => StatusCode::BAD_REQUEST,
ServiceError::InvalidCategory => StatusCode::BAD_REQUEST,
ServiceError::InvalidTag => StatusCode::BAD_REQUEST,
ServiceError::Unauthorized => StatusCode::FORBIDDEN,
ServiceError::UnauthorizedAction => StatusCode::FORBIDDEN,
ServiceError::InfoHashAlreadyExists => StatusCode::BAD_REQUEST,
ServiceError::CanonicalInfoHashAlreadyExists => StatusCode::CONFLICT,
ServiceError::OriginalInfoHashAlreadyExists => StatusCode::CONFLICT,
Expand Down
6 changes: 4 additions & 2 deletions src/services/authorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ impl Service {

let enforcer = self.casbin_enforcer.enforcer.read().await;

let authorize = enforcer.enforce((role, action)).map_err(|_| ServiceError::Unauthorized)?;
let authorize = enforcer
.enforce((role, action))
.map_err(|_| ServiceError::UnauthorizedAction)?;

if authorize {
Ok(())
} else {
Err(ServiceError::Unauthorized)
Err(ServiceError::UnauthorizedAction)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ impl Index {
// Check if user is owner or administrator
// todo: move this to an authorization service.
if !(torrent_listing.uploader == updater.username || updater.administrator) {
return Err(ServiceError::Unauthorized);
return Err(ServiceError::UnauthorizedAction);
}

self.torrent_info_repository
Expand Down

0 comments on commit 42fb031

Please sign in to comment.