Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create new response on each HTTPException #7918

Closed
wants to merge 1 commit into from

Conversation

bdraco
Copy link
Member

@bdraco bdraco commented Nov 27, 2023

What do these changes do?

This is a limited backport of #5197 and #3462

Are there changes in behavior for the user?

Related issue number

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
    • The format is <Name> <Surname>.
    • Please keep alphabetical order, the file is sorted by names.
  • Add a new news fragment into the CHANGES folder
    • name it <issue_id>.<type> for example (588.bugfix)
    • if you don't have an issue_id change it to the pr id after creating the pr
    • ensure type is one of the following:
      • .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.
    • Make sure to use full sentences with correct case and punctuation, for example: "Fix issue with non-ascii contents in doctest text files."

Comment on lines +456 to +459
resp = Response(
status=exc.status, reason=exc.reason, text=exc.text, headers=exc.headers
)
resp._cookies = exc._cookies
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a strong feeling this code is rather fiddly and probably shouldn't be touched..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pulled it directly from https://github.com/aio-libs/aiohttp/pull/5197/files#diff-2126b277e07e3fdd05e7a81da456accf24e5515a46c78c48a79db4eb166043e4R463

I'm also more than happy to close this and give up the optimization for 404s until 4.x

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that PR is making the exceptions compatible with cookies. As they are still Responses in 3.x we don't need those changes. I also recall needing to fix a problem in this code, and remember it being complex due to the Response/HTTPException thing, so I'd suggest just leaving this one.

Copy link
Member Author

@bdraco bdraco Nov 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so I'd suggest just leaving this one.

Let's close this than. We can always come back to it if the performance of the 404 turns out to be a problem but I don't think it will

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To expand on why we need to keep this for backwards-compatibility, we have, for example, this error middleware example:
https://demos.aiohttp.org/en/latest/tutorial.html#middlewares

You'll see this catches HTTPException in order to customise error responses. Older examples would have checked the type of the returned response, as these weren't raised as exceptions previously.

So, in v4 they are exceptions only, but in 3.x they must still be a hybrid Response, otherwise those old middlewares etc. that users wrote will break badly.

Comment on lines +121 to +127
@property
def status(self) -> int:
return self.status_code

@property
def headers(self) -> "CIMultiDict[str]":
return self._headers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this should already be present from Response. It was added in the linked PR due to it being separated from Response.

Copy link

codecov bot commented Nov 27, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (aa7b121) 97.35% compared to head (a4adb05) 97.35%.
Report is 2 commits behind head on 3.10.

Additional details and impacted files
@@           Coverage Diff           @@
##             3.10    #7918   +/-   ##
=======================================
  Coverage   97.35%   97.35%           
=======================================
  Files         108      108           
  Lines       32459    32467    +8     
  Branches     3849     3851    +2     
=======================================
+ Hits        31600    31608    +8     
  Misses        655      655           
  Partials      204      204           
Flag Coverage Δ
CI-GHA 97.26% <100.00%> (+<0.01%) ⬆️
OS-Linux 96.95% <100.00%> (+<0.01%) ⬆️
OS-Windows 94.45% <100.00%> (-0.01%) ⬇️
OS-macOS 96.76% <100.00%> (+0.19%) ⬆️
Py-3.10.11 94.35% <100.00%> (+<0.01%) ⬆️
Py-3.10.13 96.72% <100.00%> (-0.01%) ⬇️
Py-3.11.6 96.49% <100.00%> (+0.03%) ⬆️
Py-3.8.10 94.33% <100.00%> (-0.01%) ⬇️
Py-3.8.18 96.66% <100.00%> (+<0.01%) ⬆️
Py-3.9.13 94.34% <100.00%> (+<0.01%) ⬆️
Py-3.9.18 96.71% <100.00%> (+<0.01%) ⬆️
Py-pypy7.3.13 96.17% <100.00%> (+<0.01%) ⬆️
VM-macos 96.76% <100.00%> (+0.19%) ⬆️
VM-ubuntu 96.95% <100.00%> (+<0.01%) ⬆️
VM-windows 94.45% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bdraco bdraco closed this Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants