Catch exceptions thrown when calling API
This commit is contained in:
parent
3b06134aab
commit
6160f83045
@ -1,8 +1,11 @@
|
||||
from i3pystatus import IntervalModule
|
||||
import requests
|
||||
import json
|
||||
|
||||
import requests
|
||||
from i3pystatus import IntervalModule
|
||||
from i3pystatus import logger
|
||||
from i3pystatus.core import ConfigError
|
||||
from i3pystatus.core.util import user_open, internet, require
|
||||
from requests import Timeout, ConnectionError
|
||||
|
||||
|
||||
class Github(IntervalModule):
|
||||
@ -51,19 +54,23 @@ class Github(IntervalModule):
|
||||
|
||||
@require(internet)
|
||||
def run(self):
|
||||
format_values = dict(unread_count='', unread='')
|
||||
|
||||
if self.access_token:
|
||||
response = requests.get('https://api.github.com/notifications?access_token=' + self.access_token)
|
||||
else:
|
||||
response = requests.get('https://api.github.com/notifications', auth=(self.username, self.password))
|
||||
data = json.loads(response.text)
|
||||
try:
|
||||
if self.access_token:
|
||||
response = requests.get('https://api.github.com/notifications?access_token=' + self.access_token)
|
||||
else:
|
||||
response = requests.get('https://api.github.com/notifications', auth=(self.username, self.password))
|
||||
data = json.loads(response.text)
|
||||
except (ConnectionError, Timeout) as e:
|
||||
logger.warn(e)
|
||||
data = []
|
||||
|
||||
# Bad credentials
|
||||
if isinstance(data, dict):
|
||||
err_msg = data['message']
|
||||
raise ConfigError(err_msg)
|
||||
|
||||
format_values = dict(unread_count='', unread='')
|
||||
unread = len(data)
|
||||
if unread > 0:
|
||||
format_values['unread_count'] = unread
|
||||
|
Loading…
Reference in New Issue
Block a user