Skip to content

Commit 960a69a

Browse files
authored
feat(error): add Error::is_parse_too_large and Error::is_parse_status methods (#2538)
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.
1 parent b9916c4 commit 960a69a

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

src/error.rs

+11
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,17 @@ impl Error {
132132
matches!(self.inner.kind, Kind::Parse(_))
133133
}
134134

135+
/// Returns true if this was an HTTP parse error caused by a message that was too large.
136+
pub fn is_parse_too_large(&self) -> bool {
137+
matches!(self.inner.kind, Kind::Parse(Parse::TooLarge))
138+
}
139+
140+
/// Returns true if this was an HTTP parse error caused by an invalid response status code or
141+
/// reason phrase.
142+
pub fn is_parse_status(&self) -> bool {
143+
matches!(self.inner.kind, Kind::Parse(Parse::Status))
144+
}
145+
135146
/// Returns true if this error was caused by user code.
136147
pub fn is_user(&self) -> bool {
137148
matches!(self.inner.kind, Kind::User(_))

tests/client.rs

+77
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,83 @@ test! {
907907

908908
}
909909

910+
test! {
911+
name: client_error_parse_too_large,
912+
913+
server:
914+
expected: "\
915+
GET /err HTTP/1.1\r\n\
916+
host: {addr}\r\n\
917+
\r\n\
918+
",
919+
reply: {
920+
let long_header = std::iter::repeat("A").take(500_000).collect::<String>();
921+
format!("\
922+
HTTP/1.1 200 OK\r\n\
923+
{}: {}\r\n\
924+
\r\n\
925+
",
926+
long_header,
927+
long_header,
928+
)
929+
},
930+
931+
client:
932+
request: {
933+
method: GET,
934+
url: "http://{addr}/err",
935+
},
936+
// should get a Parse(TooLarge) error
937+
error: |err| err.is_parse() && err.is_parse_too_large(),
938+
939+
}
940+
941+
test! {
942+
name: client_error_parse_status_out_of_range,
943+
944+
server:
945+
expected: "\
946+
GET /err HTTP/1.1\r\n\
947+
host: {addr}\r\n\
948+
\r\n\
949+
",
950+
reply: "\
951+
HTTP/1.1 001 OK\r\n\
952+
\r\n\
953+
",
954+
955+
client:
956+
request: {
957+
method: GET,
958+
url: "http://{addr}/err",
959+
},
960+
// should get a Parse(Status) error
961+
error: |err| err.is_parse() && err.is_parse_status(),
962+
}
963+
964+
test! {
965+
name: client_error_parse_status_syntactically_invalid,
966+
967+
server:
968+
expected: "\
969+
GET /err HTTP/1.1\r\n\
970+
host: {addr}\r\n\
971+
\r\n\
972+
",
973+
reply: "\
974+
HTTP/1.1 1 OK\r\n\
975+
\r\n\
976+
",
977+
978+
client:
979+
request: {
980+
method: GET,
981+
url: "http://{addr}/err",
982+
},
983+
// should get a Parse(Status) error
984+
error: |err| err.is_parse() && err.is_parse_status(),
985+
}
986+
910987
test! {
911988
name: client_100_continue,
912989

0 commit comments

Comments
 (0)