[tor-commits] [ooni-probe/master] Better error handling inside of geoip code

art at torproject.org art at torproject.org
Fri Jan 3 13:29:42 UTC 2014


commit 7023511a76c492741e995bbb1cb901de0780ad8d
Author: Arturo Filastò <art at fuffa.org>
Date:   Fri Dec 27 17:24:48 2013 +0100

    Better error handling inside of geoip code
---
 ooni/geoip.py |   19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/ooni/geoip.py b/ooni/geoip.py
index 796401b..33766f3 100644
--- a/ooni/geoip.py
+++ b/ooni/geoip.py
@@ -33,19 +33,26 @@ def IPToLocation(ipaddr):
     location = {'city': None, 'countrycode': None, 'asn': None}
     try:
         city_dat = GeoIP(city_file)
-        location['city'] = city_dat.record_by_addr(ipaddr)['city']
-
+        try:
+            location['city'] = city_dat.record_by_addr(ipaddr)['city']
+        except TypeError:
+            location['city'] = None
+    
         country_dat = GeoIP(country_file)
         location['countrycode'] = country_dat.country_code_by_addr(ipaddr)
 
         asn_dat = GeoIP(asn_file)
-        location['asn'] = asn_dat.org_by_addr(ipaddr).split(' ')[0]
+        try:
+            location['asn'] = asn_dat.org_by_addr(ipaddr).split(' ')[0]
+        except AttributeError:
+            location['asn'] = 'AS0'
 
     except IOError:
-        log.err("Could not find GeoIP data files. Go into data/ "
-                "and run make geoip")
+        log.err("Could not find GeoIP data files. Go into %s "
+                "and run make geoip or change the geoip_data_dir "
+                "in the config file" % config.advanced.geoip_data_dir)
         raise GeoIPDataFilesNotFound
-
+    
     return location
 
 class HTTPGeoIPLookupper(object):





More information about the tor-commits mailing list