Skip to content

A get with an invalid port should wrap urllib3's LocationParseError exception with our own. #548

Merged
merged 1 commit into from Apr 12, 2012
View
5 requests/exceptions.py
@@ -35,4 +35,7 @@ class MissingSchema(RequestException, ValueError):
"""The URL schema (e.g. http or https) is missing."""
class InvalidSchema(RequestException, ValueError):
- """See defaults.py for valid schemas."""
+ """See defaults.py for valid schemas."""
+
+class InvalidURL(RequestException, ValueError):
+ """ The URL provided was somehow invalid. """
View
17 requests/models.py
@@ -16,15 +16,15 @@
from .auth import HTTPBasicAuth, HTTPProxyAuth
from .packages.urllib3.response import HTTPResponse
-from .packages.urllib3.exceptions import MaxRetryError
+from .packages.urllib3.exceptions import MaxRetryError, LocationParseError
from .packages.urllib3.exceptions import SSLError as _SSLError
from .packages.urllib3.exceptions import HTTPError as _HTTPError
from .packages.urllib3 import connectionpool, poolmanager
from .packages.urllib3.filepost import encode_multipart_formdata
from .defaults import SCHEMAS
from .exceptions import (
ConnectionError, HTTPError, RequestException, Timeout, TooManyRedirects,
- URLRequired, SSLError, MissingSchema, InvalidSchema)
+ URLRequired, SSLError, MissingSchema, InvalidSchema, InvalidURL)
from .utils import (
get_encoding_from_headers, stream_untransfer, guess_filename, requote_uri,
dict_from_string, stream_decode_response_unicode, get_netrc_auth)
@@ -497,11 +497,14 @@ def send(self, anyway=False, prefetch=False):
self.__dict__.update(r.__dict__)
else:
# Check to see if keep_alive is allowed.
- if self.config.get('keep_alive'):
- conn = self._poolmanager.connection_from_url(url)
- else:
- conn = connectionpool.connection_from_url(url)
-
+ try:
+ if self.config.get('keep_alive'):
+ conn = self._poolmanager.connection_from_url(url)
+ else:
+ conn = connectionpool.connection_from_url(url)
+ except LocationParseError as e:
+ raise InvalidURL(e)
+
if url.startswith('https') and self.verify:
cert_loc = None
View
7 tests/test_requests.py
@@ -781,6 +781,13 @@ def test_upload_binary_data(self):
requests.get(httpbin('post'), auth=('a', 'b'), data='\xff')
+ def test_useful_exception_for_invalid_port(self):
+ # If we pass a legitimate URL with an invalid port, we should fail.
+ self.assertRaises(
+ ValueError,
+ get,
+ 'http://google.com:banana')
+
def test_useful_exception_for_invalid_scheme(self):
# If we pass a legitimate URL with a scheme not supported
Something went wrong with that request. Please try again.