Skip to content

Commit

Permalink
Merge #1114: Improve HTTP announce error message
Browse files Browse the repository at this point in the history
3af1928 fix: [#1104] improve HTTP announce error message (Binlogo)

Pull request description:

  - Updated error messages in test cases to include "Bad request" for better clarity when query parameters cannot be parsed.
  - Modified the `From` implementations for `ParseQueryError` and `ParseAnnounceQueryError` to include "Bad request" in the error messages.

ACKs for top commit:
  josecelano:
    ACK 3af1928

Tree-SHA512: 87221cd34c73f7b9888df06957204908ac59d9a9f49052180994dcf5f45731412e94164849ab4ad45533a544c93b4f172af4a0ce8babe288cd036f1c4f8668cb
  • Loading branch information
josecelano committed Dec 2, 2024
2 parents 17e86c6 + 3af1928 commit 7faa54c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/servers/http/v1/extractors/announce_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
//! Missing query params for `announce` request: <http://0.0.0.0:7070/announce>
//!
//! ```text
//! d14:failure reason149:Cannot parse query params for announce request: missing query params for announce request in src/servers/http/v1/extractors/announce_request.rs:54:23e
//! d14:failure reason149:Bad request. Cannot parse query params for announce request: missing query params for announce request in src/servers/http/v1/extractors/announce_request.rs:54:23e
//! ```
//!
//! Invalid query param (`info_hash`): <http://0.0.0.0:7070/announce?info_hash=invalid&peer_addr=2.137.87.41&downloaded=0&uploaded=0&peer_id=-qB00000000000000001&port=17548&left=0&event=completed&compact=0>
//!
//! ```text
//! d14:failure reason240:Cannot parse query params for announce request: invalid param value invalid for info_hash in not enough bytes for infohash: got 7 bytes, expected 20 src/shared/bit_torrent/info_hash.rs:240:27, src/servers/http/v1/requests/announce.rs:182:42e
//! d14:failure reason240:Bad request. Cannot parse query params for announce request: invalid param value invalid for info_hash in not enough bytes for infohash: got 7 bytes, expected 20 src/shared/bit_torrent/info_hash.rs:240:27, src/servers/http/v1/requests/announce.rs:182:42e
//! ```
use std::panic::Location;

Expand Down Expand Up @@ -137,7 +137,7 @@ mod tests {

assert_error_response(
&response,
"Cannot parse query params for announce request: missing query params for announce request",
"Bad request. Cannot parse query params for announce request: missing query params for announce request",
);
}

Expand All @@ -146,13 +146,13 @@ mod tests {
let invalid_query = "param1=value1=value2";
let response = extract_announce_from(Some(invalid_query)).unwrap_err();

assert_error_response(&response, "Cannot parse query params");
assert_error_response(&response, "Bad request. Cannot parse query params");
}

#[test]
fn it_should_reject_a_request_with_a_query_that_cannot_be_parsed_into_an_announce_request() {
let response = extract_announce_from(Some("param1=value1")).unwrap_err();

assert_error_response(&response, "Cannot parse query params for announce request");
assert_error_response(&response, "Bad request. Cannot parse query params for announce request");
}
}
10 changes: 5 additions & 5 deletions src/servers/http/v1/extractors/scrape_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
//! Missing query params for scrape request: <http://0.0.0.0:7070/scrape>
//!
//! ```text
//! d14:failure reason143:Cannot parse query params for scrape request: missing query params for scrape request in src/servers/http/v1/extractors/scrape_request.rs:52:23e
//! d14:failure reason143:Bad request. Cannot parse query params for scrape request: missing query params for scrape request in src/servers/http/v1/extractors/scrape_request.rs:52:23e
//! ```
//!
//! Invalid query params for scrape request: <http://0.0.0.0:7070/scrape?info_hash=invalid>
//!
//! ```text
//! d14:failure reason235:Cannot parse query params for scrape request: invalid param value invalid for info_hash in not enough bytes for infohash: got 7 bytes, expected 20 src/shared/bit_torrent/info_hash.rs:240:27, src/servers/http/v1/requests/scrape.rs:66:46e
//! d14:failure reason235:Bad request. Cannot parse query params for scrape request: invalid param value invalid for info_hash in not enough bytes for infohash: got 7 bytes, expected 20 src/shared/bit_torrent/info_hash.rs:240:27, src/servers/http/v1/requests/scrape.rs:66:46e
//! ```
use std::panic::Location;

Expand Down Expand Up @@ -158,7 +158,7 @@ mod tests {

assert_error_response(
&response,
"Cannot parse query params for scrape request: missing query params for scrape request",
"Bad request. Cannot parse query params for scrape request: missing query params for scrape request",
);
}

Expand All @@ -167,13 +167,13 @@ mod tests {
let invalid_query = "param1=value1=value2";
let response = extract_scrape_from(Some(invalid_query)).unwrap_err();

assert_error_response(&response, "Cannot parse query params");
assert_error_response(&response, "Bad request. Cannot parse query params");
}

#[test]
fn it_should_reject_a_request_with_a_query_that_cannot_be_parsed_into_a_scrape_request() {
let response = extract_scrape_from(Some("param1=value1")).unwrap_err();

assert_error_response(&response, "Cannot parse query params for scrape request");
assert_error_response(&response, "Bad request. Cannot parse query params for scrape request");
}
}
4 changes: 2 additions & 2 deletions src/servers/http/v1/requests/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,15 @@ impl FromStr for Compact {
impl From<ParseQueryError> for responses::error::Error {
fn from(err: ParseQueryError) -> Self {
responses::error::Error {
failure_reason: format!("Cannot parse query params: {err}"),
failure_reason: format!("Bad request. Cannot parse query params: {err}"),
}
}
}

impl From<ParseAnnounceQueryError> for responses::error::Error {
fn from(err: ParseAnnounceQueryError) -> Self {
responses::error::Error {
failure_reason: format!("Cannot parse query params for announce request: {err}"),
failure_reason: format!("Bad request. Cannot parse query params for announce request: {err}"),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/servers/http/v1/requests/scrape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub enum ParseScrapeQueryError {
impl From<ParseScrapeQueryError> for responses::error::Error {
fn from(err: ParseScrapeQueryError) -> Self {
responses::error::Error {
failure_reason: format!("Cannot parse query params for scrape request: {err}"),
failure_reason: format!("Bad request. Cannot parse query params for scrape request: {err}"),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/servers/http/asserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub async fn assert_cannot_parse_query_params_error_response(response: Response,

assert_bencoded_error(
&response.text().await.unwrap(),
&format!("Cannot parse query params{failure}"),
&format!("Bad request. Cannot parse query params{failure}"),
Location::caller(),
);
}
Expand Down

0 comments on commit 7faa54c

Please sign in to comment.