Skip to content

Commit defc3b2

Browse files
authored
Merge pull request #130 from hsorby/main
Add exception protection around geolocate request.
2 parents e313a94 + 7bbf066 commit defc3b2

1 file changed

Lines changed: 18 additions & 13 deletions

File tree

src/mapclient/core/metrics.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ def _log_event(self, events):
105105
logger.info(f"Event response: {event['name']} - {response.status_code}")
106106
except requests.ConnectionError:
107107
for event in events:
108-
logger.info(f"Event logging failed: {event['name']}")
108+
logger.info(f"Event logging failed due to connection error for: {event['name']}")
109+
except requests.exceptions.RequestException:
110+
for event in events:
111+
logger.info(f"Event logging failed due to an unspecified error for: {event['name']}")
109112

110113

111114
metrics_logger = MetricsLogger()
@@ -117,17 +120,19 @@ def get_metrics_logger():
117120

118121
def _geolocate():
119122
url = 'https://ipinfo.io/json'
120-
response = requests.get(url)
121-
if response.ok:
122-
json_data = response.json()
123-
location = {
124-
'country': json_data.get('country', 'not-set'),
125-
'city': json_data.get('city', 'not-set'),
126-
}
127-
else:
128-
location = {
129-
'country': 'unknown',
130-
'city': 'unknown'
131-
}
123+
location = {
124+
'country': 'unknown',
125+
'city': 'unknown'
126+
}
127+
try:
128+
response = requests.get(url)
129+
if response.ok:
130+
json_data = response.json()
131+
location = {
132+
'country': json_data.get('country', 'not-set'),
133+
'city': json_data.get('city', 'not-set'),
134+
}
135+
except requests.exceptions.RequestException as ex:
136+
logger.info(f"Geolocation request failed: {ex.strerror}")
132137

133138
return location

0 commit comments

Comments
 (0)