Skip to content

Commit

Permalink
fix: [torrust#420] proper handling for tracker API responses
Browse files Browse the repository at this point in the history
Some cases were missing when the tracker service processes the responses
from the tracker API.

Added debugging and error log.

todo: add tests
  • Loading branch information
josecelano committed Jan 3, 2024
1 parent 7620ed1 commit 70ac664
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 157 deletions.
18 changes: 11 additions & 7 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ pub enum ServiceError {
#[display(fmt = "Database error.")]
DatabaseError,

// Tracker errors
// Begin tracker errors
#[display(fmt = "Sorry, we have an error with our tracker connection.")]
TrackerOffline,

Expand All @@ -160,6 +160,10 @@ pub enum ServiceError {

#[display(fmt = "Torrent not found in tracker.")]
TorrentNotFoundInTracker,

#[display(fmt = "Invalid tracker API token.")]
InvalidTrackerToken,
// End tracker errors
}

impl From<sqlx::Error> for ServiceError {
Expand Down Expand Up @@ -244,14 +248,13 @@ impl From<TrackerAPIError> for ServiceError {
eprintln!("{e}");
match e {
TrackerAPIError::TrackerOffline => ServiceError::TrackerOffline,
TrackerAPIError::AddToWhitelistError
| TrackerAPIError::RemoveFromWhitelistError
| TrackerAPIError::RetrieveUserKeyError => ServiceError::TrackerResponseError,
TrackerAPIError::InternalServerError => ServiceError::TrackerResponseError,
TrackerAPIError::TorrentNotFound => ServiceError::TorrentNotFoundInTracker,
TrackerAPIError::MissingResponseBody | TrackerAPIError::FailedToParseTrackerResponse { body: _ } => {
ServiceError::TrackerUnknownResponse
}
TrackerAPIError::UnexpectedResponseStatus
| TrackerAPIError::MissingResponseBody
| TrackerAPIError::FailedToParseTrackerResponse { body: _ } => ServiceError::TrackerUnknownResponse,
TrackerAPIError::CannotSaveUserKey => ServiceError::DatabaseError,
TrackerAPIError::InvalidToken => ServiceError::InvalidTrackerToken,
}
}
}
Expand Down Expand Up @@ -307,6 +310,7 @@ pub fn http_status_code_for_service_error(error: &ServiceError) -> StatusCode {
ServiceError::TrackerResponseError => StatusCode::INTERNAL_SERVER_ERROR,
ServiceError::TrackerUnknownResponse => StatusCode::INTERNAL_SERVER_ERROR,
ServiceError::TorrentNotFoundInTracker => StatusCode::NOT_FOUND,
ServiceError::InvalidTrackerToken => StatusCode::INTERNAL_SERVER_ERROR,
}
}

Expand Down
Loading

0 comments on commit 70ac664

Please sign in to comment.