Skip to content

Commit

Permalink
Replaced custom HTTPFetchingError with urllib.error.URLError
Browse files Browse the repository at this point in the history
  • Loading branch information
isagalaev committed Jul 15, 2014
1 parent 2338c3c commit 8fb46f3
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 149 deletions.
5 changes: 3 additions & 2 deletions examples/discover
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
import urllib.error

from openid.consumer.discover import discover, DiscoveryFailure
from openid.fetchers import HTTPFetchingError

names = [["server_url", "Server URL "],
["local_id", "Local ID "],
Expand Down Expand Up @@ -39,7 +40,7 @@ if __name__ == "__main__":
except DiscoveryFailure, why:
print "Discovery failed:", why
print
except HTTPFetchingError, why:
except urllib.error.URLError, why:
print "HTTP request failed:", why
print
else:
Expand Down
4 changes: 2 additions & 2 deletions examples/djopenid/server/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"""

import cgi
import urllib.error

from djopenid import util
from djopenid.util import getViewURL
Expand All @@ -31,7 +32,6 @@
from openid.consumer.discover import OPENID_IDP_2_0_TYPE
from openid.extensions import sreg
from openid.extensions import pape
from openid.fetchers import HTTPFetchingError

def getOpenIDStore():
"""
Expand Down Expand Up @@ -192,7 +192,7 @@ def showDecidePage(request, openid_request):
and "Valid" or "Invalid"
except DiscoveryFailure as err:
trust_root_valid = "DISCOVERY_FAILED"
except HTTPFetchingError as err:
except urllib.error.URLError as err:
trust_root_valid = "Unreachable"

pape_request = pape.Request.fromOpenIDRequest(openid_request)
Expand Down
17 changes: 9 additions & 8 deletions openid/consumer/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@
import copy
import logging
from urllib.parse import urlparse, urldefrag, parse_qsl
import urllib.error

from openid import fetchers

Expand Down Expand Up @@ -217,7 +218,7 @@ def makeKVPost(request_message, server_url):
"""Make a Direct Request to an OpenID Provider and return the
result as a Message object.
@raises openid.fetchers.HTTPFetchingError: if an error is
@raises urllib.error.URLError: if an error is
encountered in making the HTTP post.
@rtype: L{openid.message.Message}
Expand All @@ -237,7 +238,7 @@ def _httpResponseToMessage(response, server_url):
@rtype: L{openid.message.Message}
@raises openid.fetchers.HTTPFetchingError: if the server returned a
@raises urllib.error.URLError: if the server returned a
status of other than 200 or 400.
@raises ServerError: if the server returned an OpenID error.
Expand All @@ -250,7 +251,7 @@ def _httpResponseToMessage(response, server_url):
elif response.status not in (200, 206):
fmt = 'bad status code from server %s: %s'
error_message = fmt % (server_url, response.status)
raise fetchers.HTTPFetchingError(error_message)
raise urllib.error.URLError(error_message)

return response_message

Expand Down Expand Up @@ -341,9 +342,9 @@ def begin(self, user_url, anonymous=False):
disco = Discovery(self.session, user_url, self.session_key_prefix)
try:
service = disco.getNextService(self._discover)
except fetchers.HTTPFetchingError as why:
except urllib.error.URLError as why:
raise DiscoveryFailure(
'Error fetching XRDS document: %s' % (why.why,), None)
'Error fetching XRDS document: %s' % why, None)

if service is None:
raise DiscoveryFailure(
Expand Down Expand Up @@ -1105,7 +1106,7 @@ def _checkAuth(self, message, server_url):
return False
try:
response = self._makeKVPost(request, server_url)
except (fetchers.HTTPFetchingError, ServerError) as e:
except (urllib.error.URLError, ServerError) as e:
e0 = e.args[0]
logging.exception('check_authentication failed: %s' % e0)
return False
Expand Down Expand Up @@ -1272,8 +1273,8 @@ def _requestAssociation(self, endpoint, assoc_type, session_type):

try:
response = self._makeKVPost(args, endpoint.server_url)
except fetchers.HTTPFetchingError as why:
logging.exception('openid.associate request failed: %s' % (why,))
except urllib.error.URLError as why:
logging.exception('openid.associate request failed: %s' % why)
return None

try:
Expand Down
56 changes: 4 additions & 52 deletions openid/fetchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

__all__ = ['fetch', 'getDefaultFetcher', 'setDefaultFetcher', 'HTTPResponse',
'HTTPFetcher', 'createHTTPFetcher', 'HTTPFetchingError']
'createHTTPFetcher']

import urllib.request
import urllib.error
Expand Down Expand Up @@ -56,25 +56,14 @@ def getDefaultFetcher():
return _default_fetcher


def setDefaultFetcher(fetcher, wrap_exceptions=True):
def setDefaultFetcher(fetcher):
"""Set the default fetcher
@param fetcher: The fetcher to use as the default HTTP fetcher
@type fetcher: HTTPFetcher
@param wrap_exceptions: Whether to wrap exceptions thrown by the
fetcher wil HTTPFetchingError so that they may be caught
easier. By default, exceptions will be wrapped. In general,
unwrapped fetchers are useful for debugging of fetching errors
or if your fetcher raises well-known exceptions that you would
like to catch.
@type wrap_exceptions: bool
"""
global _default_fetcher
if fetcher is None or not wrap_exceptions:
_default_fetcher = fetcher
else:
_default_fetcher = ExceptionWrappingFetcher(fetcher)
_default_fetcher = fetcher


class HTTPResponse(object):
Expand Down Expand Up @@ -102,43 +91,6 @@ def _allowedURL(url):
return parsed[0] in ('http', 'https')


class HTTPFetchingError(Exception):
"""Exception that is wrapped around all exceptions that are raised
by the underlying fetcher when using the ExceptionWrappingFetcher
@ivar why: The exception that caused this exception
"""
def __init__(self, why=None):
Exception.__init__(self, why)
self.why = why


class ExceptionWrappingFetcher:
"""Fetcher that wraps another fetcher, causing all exceptions
@cvar uncaught_exceptions: Exceptions that should be exposed to the
user if they are raised by the fetch call
"""

uncaught_exceptions = (SystemExit, KeyboardInterrupt, MemoryError)

def __init__(self, fetcher):
self.fetcher = fetcher

def fetch(self, *args, **kwargs):
try:
return self.fetcher.fetch(*args, **kwargs)
except self.uncaught_exceptions:
raise
except:
exc_cls, exc_inst = sys.exc_info()[:2]
if exc_inst is None:
# string exceptions
exc_inst = exc_cls

raise HTTPFetchingError(why=exc_inst)


class Urllib2Fetcher:
"""An C{L{HTTPFetcher}} that uses urllib2.
"""
Expand All @@ -149,7 +101,7 @@ class Urllib2Fetcher:

def fetch(self, url, body=None, headers=None):
if not _allowedURL(url):
raise ValueError('Bad URL scheme: %r' % (url,))
raise urllib.error.URLError('Bad URL scheme: %r' % url)

if headers is None:
headers = {}
Expand Down
2 changes: 1 addition & 1 deletion openid/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ def returnToVerified(self):
URL does not support Yadis discovery (and so does not
support the verification process).
@raises openid.fetchers.HTTPFetchingError: if the realm URL
@raises urllib.error.URLError: if the realm URL
is not reachable. When this is the case, the RP may be hosted
on the user's intranet.
Expand Down
37 changes: 9 additions & 28 deletions openid/test/test_consumer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import urllib.parse
import urllib.error
import time
import warnings
import pprint
Expand All @@ -22,7 +23,7 @@
from openid.yadis.discover import DiscoveryFailure
from openid.dh import DiffieHellman

from openid.fetchers import HTTPResponse, HTTPFetchingError
from openid.fetchers import HTTPResponse
from openid import fetchers
from openid.store import memstore

Expand Down Expand Up @@ -158,7 +159,7 @@ def _test_success(server_url, user_url, delegate_url, links, immediate=False):
endpoint.type_uris = [OPENID_1_1_TYPE]

fetcher = TestFetcher(None, None, assocs[0])
fetchers.setDefaultFetcher(fetcher, wrap_exceptions=False)
fetchers.setDefaultFetcher(fetcher)

def run():
trust_root = str(consumer_url, encoding="utf-8")
Expand Down Expand Up @@ -1250,7 +1251,7 @@ def setUp(self):

def tearDown(self):
CatchLogs.tearDown(self)
fetchers.setDefaultFetcher(self._orig_fetcher, wrap_exceptions=False)
fetchers.setDefaultFetcher(self._orig_fetcher)

def test_error(self):
self.fetcher.response = HTTPResponse(
Expand Down Expand Up @@ -1329,11 +1330,11 @@ def setUp(self):
self.consumer = self.consumer_class(self.store)

def test_error_404(self):
"""404 from a kv post raises HTTPFetchingError"""
"""404 from a kv post raises urllib.error.URLError"""
self.fetcher.response = HTTPResponse(
"http://some_url", 404, {'Hea': 'der'}, 'blah:blah\n')
self.assertRaises(
fetchers.HTTPFetchingError,
urllib.error.URLError,
self.consumer._makeKVPost,
Message.fromPostArgs({'mode': 'associate'}),
"http://server_url")
Expand All @@ -1343,7 +1344,7 @@ def test_error_exception_unwrapped(self):
when making associations
"""
self.fetcher = ExceptionRaisingMockFetcher()
fetchers.setDefaultFetcher(self.fetcher, wrap_exceptions=False)
fetchers.setDefaultFetcher(self.fetcher)
self.assertRaises(self.fetcher.MyException,
self.consumer._makeKVPost,
Message.fromPostArgs({'mode': 'associate'}),
Expand All @@ -1360,26 +1361,6 @@ def test_error_exception_unwrapped(self):
Message.fromPostArgs({'openid.signed': ''}),
'some://url')

def test_error_exception_wrapped(self):
"""Ensure that openid.fetchers.HTTPFetchingError is caught by
the association creation stuff.
"""
self.fetcher = ExceptionRaisingMockFetcher()
# This will wrap exceptions!
fetchers.setDefaultFetcher(self.fetcher)
self.assertRaises(fetchers.HTTPFetchingError,
self.consumer._makeKVPost,
Message.fromOpenIDArgs({'mode': 'associate'}),
"http://server_url")

# exception fetching returns no association
e = OpenIDServiceEndpoint()
e.server_url = 'some://url'
self.assertTrue(self.consumer._getAssociation(e) is None)

msg = Message.fromPostArgs({'openid.signed': ''})
self.assertFalse(self.consumer._checkAuth(msg, 'some://url'))


class TestSuccessResponse(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -1514,7 +1495,7 @@ def test_beginHTTPError(self):
"""Make sure that the discovery HTTP failure case behaves properly
"""
def getNextService(self, ignored):
raise HTTPFetchingError("Unit test")
raise urllib.error.URLError("Unit test")

def test():
try:
Expand Down Expand Up @@ -2142,7 +2123,7 @@ def test_500(self):
response = HTTPResponse()
response.status = 500
response.body = "foo:bar\nbaz:quux\n"
self.assertRaises(fetchers.HTTPFetchingError,
self.assertRaises(urllib.error.URLError,
_httpResponseToMessage, response,
self.server_url)

Expand Down
4 changes: 2 additions & 2 deletions openid/test/test_discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def __init__(self, exc):

def setUp(self):
fetcher = ErrorRaisingFetcher(self.exc)
fetchers.setDefaultFetcher(fetcher, wrap_exceptions=False)
fetchers.setDefaultFetcher(fetcher)

def tearDown(self):
fetchers.setDefaultFetcher(None)
Expand All @@ -132,7 +132,7 @@ def runOneTest(self):
class TestNormalization(unittest.TestCase):
def testAddingProtocol(self):
f = ErrorRaisingFetcher(RuntimeError())
fetchers.setDefaultFetcher(f, wrap_exceptions=False)
fetchers.setDefaultFetcher(f)

try:
discover.discover('users.stompy.janrain.com:8000/x')
Expand Down
Loading

0 comments on commit 8fb46f3

Please sign in to comment.