commit 864d001be170cb12fd5ad55faafe9518fbffce89
Author: srvetus <srvetus(a)users.noreply.github.com>
Date: Fri Sep 25 00:23:53 2015 +0200
Really handleAllFailures()
handleAllFailures() only handles failures which are explicitly named. Maybe it
should be renamed or emit a log message when an unknown failure is reraised
from `failure.trap()`
The known failure list has been converted to a list of tuples as
the order of the failure checking is important. Some Failures subclass
other failures and so they should be listed from most specific failure
to the least specific.
---
ooni/errors.py | 69 +++++++++++++++++++++++++++++-----------------------------
1 file changed, 34 insertions(+), 35 deletions(-)
diff --git a/ooni/errors.py b/ooni/errors.py
index 5751baf..84c0afc 100644
--- a/ooni/errors.py
+++ b/ooni/errors.py
@@ -20,33 +20,33 @@ from txsocksx.errors import TTLExpired, CommandNotSupported
from socket import gaierror
-known_failures = {
- ConnectionRefusedError: 'connection_refused_error',
- ConnectionLost: 'connection_lost_error',
- ConnectError: 'connect_error',
- CancelledError: 'task_timed_out',
- gaierror: 'address_family_not_supported_error',
- DNSLookupError: 'dns_lookup_error',
- TCPTimedOutError: 'tcp_timed_out_error',
- ResponseNeverReceived: 'response_never_received',
- DeferTimeoutError: 'deferred_timeout_error',
- GenericTimeoutError: 'generic_timeout_error',
- SOCKSError: 'socks_error',
- MethodsNotAcceptedError: 'socks_methods_not_supported',
- AddressNotSupported: 'socks_address_not_supported',
- NetworkUnreachable: 'socks_network_unreachable',
- ConnectionError: 'socks_connect_error',
- ConnectionLostEarly: 'socks_connection_lost_early',
- ConnectionNotAllowed: 'socks_connection_not_allowed',
- NoAcceptableMethods: 'socks_no_acceptable_methods',
- ServerFailure: 'socks_server_failure',
- HostUnreachable: 'socks_host_unreachable',
- ConnectionRefused: 'socks_connection_refused',
- TTLExpired: 'socks_ttl_expired',
- CommandNotSupported: 'socks_command_not_supported',
- ProcessDone: 'process_done',
- ConnectionDone: 'connection_done',
-}
+known_failures = [
+ (ConnectionRefusedError, 'connection_refused_error'),
+ (ConnectionLost, 'connection_lost_error'),
+ (CancelledError, 'task_timed_out'),
+ (gaierror, 'address_family_not_supported_error'),
+ (DNSLookupError, 'dns_lookup_error'),
+ (TCPTimedOutError, 'tcp_timed_out_error'),
+ (ResponseNeverReceived, 'response_never_received'),
+ (DeferTimeoutError, 'deferred_timeout_error'),
+ (GenericTimeoutError, 'generic_timeout_error'),
+ (SOCKSError, 'socks_error'),
+ (MethodsNotAcceptedError, 'socks_methods_not_supported'),
+ (AddressNotSupported, 'socks_address_not_supported'),
+ (NetworkUnreachable, 'socks_network_unreachable'),
+ (ConnectionError, 'socks_connect_error'),
+ (ConnectionLostEarly, 'socks_connection_lost_early'),
+ (ConnectionNotAllowed, 'socks_connection_not_allowed'),
+ (NoAcceptableMethods, 'socks_no_acceptable_methods'),
+ (ServerFailure, 'socks_server_failure'),
+ (HostUnreachable, 'socks_host_unreachable'),
+ (ConnectionRefused, 'socks_connection_refused'),
+ (TTLExpired, 'socks_ttl_expired'),
+ (CommandNotSupported, 'socks_command_not_supported'),
+ (ProcessDone, 'process_done'),
+ (ConnectionDone, 'connection_done'),
+ (ConnectError, 'connect_error'),
+]
def handleAllFailures(failure):
"""
@@ -55,9 +55,7 @@ def handleAllFailures(failure):
returned by failure.trap().
"""
- # TODO: Should this function actually handle ALL failures and
- # not just the failures configured in this list.
- failure.trap(*known_failures.keys())
+ failure.trap(*[failure_type for failure_type, _ in known_failures])
return failureToString(failure)
@@ -75,12 +73,13 @@ def failureToString(failure):
A string representing the HTTP response error message.
"""
- for failure_type, failure_string in known_failures.items():
+ for failure_type, failure_string in known_failures:
if isinstance(failure.value, failure_type):
- return failure_string
-
- # Did not find a matching failure message
- return 'unknown_failure %s' % str(failure.value)
+ if failure_string:
+ return failure_string
+ else:
+ # Failure without a corresponding failure message
+ return 'unknown_failure %s' % str(failure.value)
class DirectorException(Exception):
pass