Skip to content

Commit

Permalink
Gracefully handle reverse geocoding failures
Browse files Browse the repository at this point in the history
  • Loading branch information
cnorthwood committed Jul 8, 2011
1 parent 9277a89 commit 5a07508
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions molly/geolocation/providers/cloudmade.py
Expand Up @@ -26,12 +26,23 @@ def reverse_geocode(self, lon, lat):
'type': 'road',
}

data = urllib2.urlopen(self.REVERSE_GEOCODE_URL % params)

json = simplejson.load(data)
try:
request_url = self.REVERSE_GEOCODE_URL % params
response = urllib2.urlopen(request_url)
if response.code != 200:
logger.error("Request to %s returned response code %d" % (request_url, response.code))
return []
json = simplejson.loads(response.read().replace(''', "'"), 'utf8')
except urllib2.HTTPError, e:
logger.error("Cloudmade returned a non-OK response code %d", e.code)
return []
except urllib2.URLError, e:
logger.error("Encountered an error reaching Cloudmade: %s", str(e))
return []

if not json:
return []

else:
name = json['features'][0]['properties'].get('name')
try:
Expand Down

0 comments on commit 5a07508

Please sign in to comment.