[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [oonib/master] Properly write error messages.



commit 4210908e8853cd81e5945a5c09cc943a8f28bec4
Author: Arturo Filastò <art@xxxxxxxxx>
Date:   Tue Aug 27 13:34:05 2013 +0200

    Properly write error messages.
    
    Update the /bouncer API to reflect spec changes
---
 oonib/bouncer/handlers.py |   60 +++++++++++++++++++++++++++++++++------------
 oonib/errors.py           |    3 ++-
 oonib/handlers.py         |    5 ++++
 3 files changed, 52 insertions(+), 16 deletions(-)

diff --git a/oonib/bouncer/handlers.py b/oonib/bouncer/handlers.py
index e669e8e..9e987f0 100644
--- a/oonib/bouncer/handlers.py
+++ b/oonib/bouncer/handlers.py
@@ -36,7 +36,7 @@ class Bouncer(object):
         try:
             helpers = self.knownHelpers[helper_name]
         except KeyError:
-            raise e.NoHelperFound
+            raise e.TestHelperNotFound
         
         helpers_dict = {}
         for helper in helpers:
@@ -52,15 +52,26 @@ class Bouncer(object):
         requested_helpers = ['a', 'b', 'c']
         will return:
          {
-            'a': '127.0.0.1',
-            'b': 'http://127.0.0.1',
-            'c': '127.0.0.1:590',
-            'collector': 'httpo://thirteenchars1.onion'
+            'a': {
+                'address': '127.0.0.1',
+                'collector': 'httpo://thirteenchars1.onion'
+            },
+            'b': {
+                'address': '127.0.0.1:8081',
+                'collector': 'httpo://thirteenchars1.onion'
+            },
+            'c': {
+                'address': 'http://127.0.0.1',
+                'collector': 'httpo://thirteenchars2.onion'
+            },
+            'default': {
+                'collector': 'httpo://thirteenchars1.onion'
+            }
          }
 
          or 
 
-         {}
+         {'error': 'test-helper-not-found'}
 
          if no valid helper was found
 
@@ -72,18 +83,37 @@ class Bouncer(object):
                     result[collector] = {}
                 result[collector][helper_name] = helper_address
 
-        helper_list = []
+        # {
+        #   'foo.onion': {'some-helper': 'some-address'},
+        #   'foo2.onion': {'some-helper': 'some-addres2'}
+        # }
+
+        response = {}
+        default_collector = None
+        max_helper_count = 0
         for collector, helpers in result.items():
-            if len(helpers) == len(requested_helpers):
-                valid_helpers = helpers
-                valid_helpers['collector'] = collector
-                helper_list.append(valid_helpers)
+            if len(helpers) > max_helper_count:
+                default_collector = collector
             else:
                 continue
-        if len(helper_list) == 0:
-            return {}
-        else:
-            return random.choice(helper_list)
+
+        response['default'] = {}
+        response['default']['collector'] = default_collector
+
+        if not len(result[default_collector]) == len(requested_helpers):
+            found_helpers = set(result[default_collector].items())
+            for missing_helper in found_helpers.difference(requested_helpers):
+                collector, address = random.choice(self.getHelperAddress(missing_helper).items())
+                response[missing_helper] = {}
+                response[missing_helper]['collector'] = collector
+                response[missing_helper]['address'] = address
+
+        for name, address in result[default_collector].items():
+            response[name] = {}
+            response[name]['address'] = address
+            response[name]['collector'] = default_collector
+        
+        return response
 
 class BouncerQueryHandler(OONIBHandler):
     def initialize(self):
diff --git a/oonib/errors.py b/oonib/errors.py
index 5c4e4d8..2385a6f 100644
--- a/oonib/errors.py
+++ b/oonib/errors.py
@@ -55,5 +55,6 @@ class TestHelpersKeyMissing(OONIBError):
     log_message = "test-helpers-key-missing"
 
 class TestHelperNotFound(OONIBError):
-    status_code = 400
+    status_code = 404
     log_message = "test-helper-not-found"
+
diff --git a/oonib/handlers.py b/oonib/handlers.py
index 033a69f..2231a2a 100644
--- a/oonib/handlers.py
+++ b/oonib/handlers.py
@@ -4,6 +4,11 @@ from cyclone import escape
 from cyclone import web
 
 class OONIBHandler(web.RequestHandler):
+    def write_error(self, status_code, exception=None, **kw):
+        self.set_status(status_code)
+        if exception:
+            self.write({'error': exception.log_message})
+
     def write(self, chunk):
         """
         This is a monkey patch to RequestHandler to allow us to serialize also



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits