Skip to content

Commit

Permalink
feat(error): add predicates for too-large and status parse errors
Browse files Browse the repository at this point in the history
The discussion in #2462 opened up some larger questions about more comprehensive approaches to the
error API, with the agreement that additional methods would be desirable in the short term. These
methods address an immediate need of our customers, so I would like to get them in first before we
flesh out a future solution.

One potentially controversial choice here is to still return `true` from `is_parse_error()` for
these variants. I hope the naming of the methods make it clear that the new predicates are
refinements of the existing one, but I didn't want to change the behavior of `is_parse_error()`
which would require a major version bump.
  • Loading branch information
acfoltzer committed May 6, 2021
1 parent d1d2f32 commit 0fd4111
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ impl Error {
matches!(self.inner.kind, Kind::Parse(_))
}

/// Returns true if this was an HTTP parse error caused by a message that was too large.
pub fn is_parse_too_large(&self) -> bool {
matches!(self.inner.kind, Kind::Parse(Parse::TooLarge))
}

/// Returns true if this was an HTTP parse error caused by an invalid response status code or
/// reason phrase.
pub fn is_parse_status(&self) -> bool {
matches!(self.inner.kind, Kind::Parse(Parse::Status))
}

/// Returns true if this error was caused by user code.
pub fn is_user(&self) -> bool {
matches!(self.inner.kind, Kind::User(_))
Expand Down
77 changes: 77 additions & 0 deletions tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,83 @@ test! {

}

test! {
name: client_error_parse_too_large,

server:
expected: "\
GET /err HTTP/1.1\r\n\
host: {addr}\r\n\
\r\n\
",
reply: {
let long_header = std::iter::repeat("A").take(500_000).collect::<String>();
format!("\
HTTP/1.1 200 OK\r\n\
{}: {}\r\n\
\r\n\
",
long_header,
long_header,
)
},

client:
request: {
method: GET,
url: "http://{addr}/err",
},
// should get a Parse(Version) error
error: |err| err.is_parse() && err.is_parse_too_large(),

}

test! {
name: client_error_parse_status_out_of_range,

server:
expected: "\
GET /err HTTP/1.1\r\n\
host: {addr}\r\n\
\r\n\
",
reply: "\
HTTP/1.1 001 OK\r\n\
\r\n\
",

client:
request: {
method: GET,
url: "http://{addr}/err",
},
// should get a Parse(Status) error
error: |err| err.is_parse() && err.is_parse_status(),
}

test! {
name: client_error_parse_status_syntactically_invalid,

server:
expected: "\
GET /err HTTP/1.1\r\n\
host: {addr}\r\n\
\r\n\
",
reply: "\
HTTP/1.1 1 OK\r\n\
\r\n\
",

client:
request: {
method: GET,
url: "http://{addr}/err",
},
// should get a Parse(Status) error
error: |err| err.is_parse() && err.is_parse_status(),
}

test! {
name: client_100_continue,

Expand Down

0 comments on commit 0fd4111

Please sign in to comment.