Catch much errors

This commit is contained in:
Luke Rogers 2014-02-13 10:54:20 +13:00
parent 294a97b629
commit c2884242b3

View file

@ -10,7 +10,12 @@ def mcwiki(inp):
"""mcwiki <phrase> -- Gets the first paragraph of """mcwiki <phrase> -- Gets the first paragraph of
the Minecraft Wiki article on <phrase>.""" the Minecraft Wiki article on <phrase>."""
try:
j = http.get_json(api_url, search=inp) j = http.get_json(api_url, search=inp)
except (http.HTTPError, http.URLError) as e:
return "Error fetching search results: {}".format(e)
except ValueError as e:
return "Error reading search results: {}".format(e)
if not j[1]: if not j[1]:
return "No results found." return "No results found."
@ -27,7 +32,11 @@ def mcwiki(inp):
article_name = j[1][0].replace(' ', '_').encode('utf8') article_name = j[1][0].replace(' ', '_').encode('utf8')
url = mc_url + http.quote(article_name, '') url = mc_url + http.quote(article_name, '')
try:
page = http.get_html(url) page = http.get_html(url)
except (http.HTTPError, http.URLError) as e:
return "Error fetching wiki page: {}".format(e)
for p in page.xpath('//div[@class="mw-content-ltr"]/p'): for p in page.xpath('//div[@class="mw-content-ltr"]/p'):
if p.text_content(): if p.text_content():