Skip to content

Commit c2921a8

Browse files
committed
#2865 Try to add a bit of error handling
1 parent 313e1dd commit c2921a8

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

talkmap.ipynb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"import frontmatter\n",
3737
"import glob\n",
3838
"import getorg\n",
39-
"from geopy import Nominatim"
39+
"from geopy import Nominatim\n",
40+
"from geopy.exc import GeocoderTimedOut"
4041
]
4142
},
4243
{
@@ -59,6 +60,9 @@
5960
},
6061
"outputs": [],
6162
"source": [
63+
"# Set the default timeout, in seconds\n",
64+
"TIMEOUT = 5\n",
65+
"\n",
6266
"# Prepare to geolocate\n",
6367
"geocoder = Nominatim(user_agent=\"academicpages.github.io\")\n",
6468
"location_dict = {}\n",
@@ -99,8 +103,15 @@
99103
" description = f\"{title}<br />{venue}; {location}\"\n",
100104
"\n",
101105
" # Geocode the location and report the status\n",
102-
" location_dict[description] = geocoder.geocode(location)\n",
103-
" print(description, location_dict[description])"
106+
" try:\n",
107+
" location_dict[description] = geocoder.geocode(location, timeout=TIMEOUT)\n",
108+
" print(description, location_dict[description])\n",
109+
" except ValueError as ex:\n",
110+
" print(f\"Error: geocode failed on input {location} with message {ex}\")\n",
111+
" except GeocoderTimedOut as ex:\n",
112+
" print(f\"Error: geocode timed out on input {location} with message {ex}\")\n",
113+
" except Exception as ex:\n",
114+
" print(f\"An unhandled exception occurred while processing input {location} with message {ex}\")"
104115
]
105116
},
106117
{

talkmap.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
import glob
1010
import getorg
1111
from geopy import Nominatim
12+
from geopy.exc import GeocoderTimedOut
13+
14+
# Set the default timeout, in seconds
15+
TIMEOUT = 5
1216

1317
# Collect the Markdown files
1418
g = glob.glob("_talks/*.md")
@@ -37,8 +41,15 @@
3741
description = f"{title}<br />{venue}; {location}"
3842

3943
# Geocode the location and report the status
40-
location_dict[description] = geocoder.geocode(location)
41-
print(description, location_dict[description])
44+
try:
45+
location_dict[description] = geocoder.geocode(location, timeout=TIMEOUT)
46+
print(description, location_dict[description])
47+
except ValueError as ex:
48+
print(f"Error: geocode failed on input {location} with message {ex}")
49+
except GeocoderTimedOut as ex:
50+
print(f"Error: geocode timed out on input {location} with message {ex}")
51+
except Exception as ex:
52+
print(f"An unhandled exception occurred while processing input {location} with message {ex}")
4253

4354
# Save the map
4455
m = getorg.orgmap.create_map_obj()

0 commit comments

Comments
 (0)