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

Improve error reporting when None is mistakenly passed as a URL on client requests #3508

Closed
liberforce opened this issue Jan 9, 2019 · 4 comments

Comments

@liberforce
Copy link

Long story short

While reusing some code I didn't notice that it required an environment variable to be defined.
This led to the URL used for the requests being None. However, calling ClientSession.request with str_or_url equal to None doesn't trigger an InvalidURL exception, but a TypeError on URL(str_or_url).

https://github.com/aio-libs/aiohttp/blob/master/aiohttp/client.py#L361

Expected behaviour

Consider None as an invalid URL and raise an according exception (InvalidURL)

Actual behaviour

TypeError is rased in yarl.URL.__new__

Steps to reproduce

Call ClientSession.request(method='GET', str_or_url=None, ...)

Your environment

Python 3.7 and aiohttp 3.5.2 in a virtualenv.
Running Ubuntu 18.04.
client.

@aio-libs-bot
Copy link

GitMate.io thinks the contributor most likely able to help you is @asvetlov.

Possibly related issues are #3242 (Error), #875 (Improve Request.repr), #2865 (Improve client timeouts), #3302 (Request json raises error if no arguments passed), and #3424 (Request url of client session gets malformed).

@asvetlov
Copy link
Member

asvetlov commented Jan 9, 2019

I think TypeError is pretty common exception for invalid argument type.
InvalidURL is a kind of ValueError which is raised when the type is correct but the value is not.

>>> int("10")
10
>>> int("not-valid")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'not-valid'
>>> int(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

@liberforce
Copy link
Author

Right, it's just that having a wrong type also has the consequence of producing and invalid URL, so maybe raising an InvalidURL exception would be a better hint of what is wrong. Or maybe not, as the TypeError is more precise.

@lock
Copy link

lock bot commented Jan 11, 2020

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 Jan 11, 2020
@lock lock bot locked as resolved and limited conversation to collaborators Jan 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants