[tor-commits] [ooni-probe/master] Refactor failure handling code in nettest

art at torproject.org art at torproject.org
Sun Dec 23 22:28:11 UTC 2012


commit fe03d191b3158e1d5f305faa573a1aebefa8cff0
Author: Arturo Filastò <art at fuffa.org>
Date:   Sun Dec 23 23:26:01 2012 +0100

    Refactor failure handling code in nettest
    Add function that traps all the supported failure and outputs the failure string representing it
    * Add support for generic timeout error
---
 ooni/nettest.py |   30 +++++++++++++++++++++++++-----
 1 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/ooni/nettest.py b/ooni/nettest.py
index fcc945a..062a88f 100644
--- a/ooni/nettest.py
+++ b/ooni/nettest.py
@@ -7,8 +7,11 @@ from twisted.trial import unittest, itrial, util
 from twisted.internet import defer, utils
 from twisted.python import usage
 
-from twisted.internet.error import ConnectionRefusedError, DNSLookupError, TCPTimedOutError
-from twisted.internet.defer import TimeoutError
+from twisted.internet.error import ConnectionRefusedError, TCPTimedOutError
+from twisted.internet.error import DNSLookupError
+from twisted.internet.error import TimeoutError as GenericTimeoutError
+
+from twisted.internet.defer import TimeoutError as DeferTimeoutError
 from twisted.web._newclient import ResponseNeverReceived
 
 from ooni.utils import log
@@ -16,6 +19,17 @@ from ooni.utils.txagentwithsocks import SOCKSError
 
 from socket import gaierror
 
+def handleAllFailures(failure):
+    """
+    Here we make sure to trap all the failures that are supported by the
+    failureToString function and we return the the string that represents the
+    failure.
+    """
+    failure.trap(ConnectionRefusedError, gaierror, SOCKSError,
+        DNSLookupError, TCPTimedOutError, ResponseNeverReceived,
+        DeferTimeoutError, GenericTimeoutError)
+    return failureToString(failure)
+
 def failureToString(failure):
     """
     Given a failure instance return a string representing the kind of error
@@ -54,12 +68,18 @@ def failureToString(failure):
         log.err("Response Never Received")
         string = 'response_never_received'
 
-    elif isinstance(failure.value, TimeoutError):
-        log.err("Deferred Timed Out Error")
-        string = 'deferred_timed_out_error'
+    elif isinstance(failure.value, DeferTimeoutError):
+        log.err("Deferred Timeout Error")
+        string = 'deferred_timeout_error'
+
+    elif isinstance(failure.value, GenericTimeoutError):
+        log.err("Time Out Error")
+        string = 'generic_timeout_error'
 
     else:
         log.err("Unknown failure type: %s" % type(failure))
+        string = 'unknown_failure %s' % str(failure.value)
+
     return string
 
 class NoPostProcessor(Exception):





More information about the tor-commits mailing list