Skip to content

Commit

Permalink
Adding Fido custom exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
lucagiovagnoli committed Jul 6, 2016
1 parent 498a263 commit 2369fc6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
19 changes: 19 additions & 0 deletions fido/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-


class BaseTimeoutError(Exception):
"""
Base class for all errors due to timeouts.
"""


class ConnectTimeoutError(BaseTimeoutError):
"""
Connection took too long to establish
"""


class HTTPTimeoutError(BaseTimeoutError):
"""
Server took too long to send the response.
"""
7 changes: 4 additions & 3 deletions fido/fido.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

from . import __about__
from .common import listify_headers
from fido.exceptions import ConnectTimeoutError
from fido.exceptions import HTTPTimeoutError


##############################################################################
Expand All @@ -26,7 +28,6 @@
# ('Bad File Descriptor' exceptions).
##############################################################################


def _import_reactor():
from twisted.internet import reactor
return reactor
Expand Down Expand Up @@ -216,14 +217,14 @@ def handle_timeout_errors(error):

if error.check(_twisted_web_client().ResponseNeverReceived):
if error.value.reasons[0].check(CancelledError):
raise crochet.TimeoutError(
raise HTTPTimeoutError(
"Connection was closed by fido because the server took "
"more than timeout={timeout} seconds to "
"send the response".format(timeout=timeout)
)

elif error.check(TwistedTimeoutError):
raise crochet.TimeoutError(
raise ConnectTimeoutError(
"Connection was closed by Twisted Agent because the HTTP "
"connection took more than connect_timeout={connect_timeout} "
"seconds to establish.".format(connect_timeout=connect_timeout)
Expand Down
6 changes: 4 additions & 2 deletions tests/fido_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from fido.fido import _set_deferred_timeout
from fido.fido import _twisted_web_client
from fido.fido import DEFAULT_USER_AGENT
from fido.exceptions import ConnectTimeoutError
from fido.exceptions import HTTPTimeoutError

SERVER_OVERHEAD_TIME = 2.0
TIMEOUT_TEST = 1.0
Expand Down Expand Up @@ -144,7 +146,7 @@ def test_agent_timeout(server_url):
# EventualResult stores them and re-raises on result retrieval
assert eventual_result.original_failure() is not None

with pytest.raises(crochet.TimeoutError) as e:
with pytest.raises(HTTPTimeoutError) as e:
eventual_result.wait()

assert (
Expand Down Expand Up @@ -173,7 +175,7 @@ def test_agent_connect_timeout():
# EventualResult stores them and re-raises on result retrieval
assert eventual_result.original_failure() is not None

with pytest.raises(crochet.TimeoutError) as e:
with pytest.raises(ConnectTimeoutError) as e:
eventual_result.wait()

assert (
Expand Down

0 comments on commit 2369fc6

Please sign in to comment.