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

Raise ContentTypeError by json() when headers does not … #2136

Merged

Conversation

pfreixes
Copy link
Contributor

@pfreixes pfreixes commented Jul 27, 2017

What do these changes do?

Basically, avoid having a nocive fall back as the following one

try:
    resp = await req.json()
except ClieintResponseError:
    logging.warning("Invalid content-type")
    resp = loads(await req.read())

The ClieintResponseError can be raised by many reasons, and the case presented the fallback can be misleading having as a result twice ClieintResponseError because the source of the problem wasn't related to the content type.

With an ad-hoc exception, the client can code a more specific fallback case taking into account the differents source of problems with a cleaner interface, for example:

try:
    resp = await req.json()
except ContentTypeError:
    logging.warning("Invalid content-type by servcie ....")
    resp = loads(await req.read())
except ClieintResponseError:
    logging.exception("something went wrong")
    raise

Thanks to the current json() implementation once we have had a ContentTypeError exception is not possible to have a ClieintResponseError. Therefore, the developer can handle the errors as the previous example.

For backward compatibility the ContentTypeError inherits from ClieintResponseError.

Are there changes in behavior for the user?

No

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.bug)
    • 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."

@codecov-io
Copy link

codecov-io commented Jul 27, 2017

Codecov Report

Merging #2136 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2136      +/-   ##
==========================================
+ Coverage   97.07%   97.07%   +<.01%     
==========================================
  Files          38       38              
  Lines        7723     7728       +5     
  Branches     1346     1347       +1     
==========================================
+ Hits         7497     7502       +5     
  Misses        102      102              
  Partials      124      124
Impacted Files Coverage Δ
aiohttp/client_exceptions.py 100% <100%> (ø) ⬆️
aiohttp/client_reqrep.py 97.19% <100%> (ø) ⬆️
tests/autobahn/client.py 96.84% <0%> (ø) ⬆️
aiohttp/web_urldispatcher.py 99.42% <0%> (ø) ⬆️
aiohttp/web.py 99.65% <0%> (ø) ⬆️
aiohttp/test_utils.py 98.6% <0%> (ø) ⬆️
aiohttp/web_request.py 98.94% <0%> (+0.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 91dc5b7...0f7b655. Read the comment docs.

@asvetlov
Copy link
Member

The intention is pretty clean but ClientResponseContentTypeError is 30 chars long!
Please invite shorter name.

@pfreixes pfreixes changed the title Raise ClientResponseContentTypeError by json() when headers does not … Raise ContentTypeError by json() when headers does not … Jul 27, 2017
@pfreixes
Copy link
Contributor Author

Exception name changed to ContentTypeError

@asvetlov asvetlov merged commit ecc8e63 into aio-libs:master Jul 27, 2017
@asvetlov
Copy link
Member

Thanks!

@lock
Copy link

lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bot:chronographer:provided There is a change note present in this PR outdated
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants