Clarify LineTooLong error messages with context label#12393
Clarify LineTooLong error messages with context label#12393Ricardo-M-L wants to merge 3 commits intoaio-libs:masterfrom
LineTooLong error messages with context label#12393Conversation
The `LineTooLong` exception now accepts an optional `context` parameter describing which part of the HTTP message exceeded the size limit (e.g. `"request URL"`, `"status reason phrase"`, `"header field name"`, `"header field value"`, `"request/status line"`, `"trailer line"`, `"chunk size line"`, `"header line with continuations"`, or `"stream until separator"`). Every `LineTooLong` call site in the pure-Python parser, the Cython parser, and `StreamReader.readuntil` now passes a descriptive context. Fixes the confusing "Status line is too long" error reported in aio-libs#7177 when the actual issue is a too-long URL. Co-authored-by: Claude <[email protected]>
for more information, see https://pre-commit.ci
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #12393 +/- ##
=======================================
Coverage 98.92% 98.92%
=======================================
Files 134 134
Lines 46616 46639 +23
Branches 2429 2430 +1
=======================================
+ Hits 46114 46137 +23
Misses 373 373
Partials 129 129
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Merging this PR will not alter performance
Comparing Footnotes
|
Sphinx nitpicky mode can't resolve :exc:`~aiohttp.http_exceptions.LineTooLong` since the exception isn't autodoc'd. Match the convention from other CHANGES entries (e.g. 10611.bugfix.rst) and use plain inline code instead.
|
I think the existing exception is clear enough, as it shows the actual line. The linked issue refers to "Status line is too long", which appears to have been removed in 3.13. |
What do these changes do?
Closes #7177.
The
LineTooLongexception previously produced a generic error message:…regardless of which part of the HTTP message actually overflowed the
limit. That was especially confusing for long URLs, where older versions
labelled the overflow as a "Status line is too long" (see the logs in
#7177). Even after that text was removed, the resulting message still
gave no hint about whether the URL, the status reason, a header, or a
trailer was at fault.
This PR adds an optional
contextparameter toaiohttp.http_exceptions.LineTooLongand wires it through every callsite so the message now looks like:
The addition is fully backwards-compatible: the parameter defaults to
"line", so existing call-sites that have not been updated stillproduce a coherent (if slightly less specific) message.
Changes
aiohttp/http_exceptions.py– accept and displaycontextinLineTooLong.aiohttp/_http_parser.pyx– pass"request URL"/"status reason phrase"/"header field name"/"header field value"from the Cython callbacks.aiohttp/http_parser.py– pass"request/status line"/"header line with continuations"/"chunk size line"/"trailer line"from the pure-Python parser paths.aiohttp/streams.py– labelStreamReader.readuntiloverflows as"stream until separator".Tests
tests/test_http_exceptions.py::TestLineTooLong– addedtest_ctor_with_contextandtest_pickle_with_context, updated theexisting message assertions for the new default label.
tests/test_http_parser.py::test_http_request_max_status_line_mentions_request_line– new regression test asserting that a too-long request line is
described as such in the error message.
All existing
match=...regex assertions anchor on the prefix"Got more than N bytes when reading"(no trailing colon), so theycontinue to pass unchanged.
Are there changes in behavior for the user?
The wording of the
LineTooLongerror message changes to include acontext label. Code that parses the message string could need updates;
code that catches the exception or reads
err.args = (line, limit, context)is unaffected.
Related issue number
Closes #7177.
Checklist
CHANGES/folder<issue_id>.<type>.rst(e.g.588.bugfix.rst)issue_id, change it to the PR number aftercreating the PR
.feature: Signifying a new feature..bugfix: Signifying a bug fix..doc: Signifying a documentation improvement..removal: Signifying a deprecation or removal of public API..misc: A ticket has been closed, but it is not of interest to users.for example: "Fix issue with non-ascii contents in doctest text files."
Disclosure
AI assistance (Claude) was used to draft these changes. I reviewed every
line and ran the relevant tests locally.