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 json
|
||||||
|
|
||||||
|
import requests
|
||||||
|
from i3pystatus import IntervalModule
|
||||||
|
from i3pystatus import logger
|
||||||
from i3pystatus.core import ConfigError
|
from i3pystatus.core import ConfigError
|
||||||
from i3pystatus.core.util import user_open, internet, require
|
from i3pystatus.core.util import user_open, internet, require
|
||||||
|
from requests import Timeout, ConnectionError
|
||||||
|
|
||||||
|
|
||||||
class Github(IntervalModule):
|
class Github(IntervalModule):
|
||||||
@ -51,19 +54,23 @@ class Github(IntervalModule):
|
|||||||
|
|
||||||
@require(internet)
|
@require(internet)
|
||||||
def run(self):
|
def run(self):
|
||||||
format_values = dict(unread_count='', unread='')
|
|
||||||
|
|
||||||
if self.access_token:
|
try:
|
||||||
response = requests.get('https://api.github.com/notifications?access_token=' + self.access_token)
|
if self.access_token:
|
||||||
else:
|
response = requests.get('https://api.github.com/notifications?access_token=' + self.access_token)
|
||||||
response = requests.get('https://api.github.com/notifications', auth=(self.username, self.password))
|
else:
|
||||||
data = json.loads(response.text)
|
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
|
# Bad credentials
|
||||||
if isinstance(data, dict):
|
if isinstance(data, dict):
|
||||||
err_msg = data['message']
|
err_msg = data['message']
|
||||||
raise ConfigError(err_msg)
|
raise ConfigError(err_msg)
|
||||||
|
|
||||||
|
format_values = dict(unread_count='', unread='')
|
||||||
unread = len(data)
|
unread = len(data)
|
||||||
if unread > 0:
|
if unread > 0:
|
||||||
format_values['unread_count'] = unread
|
format_values['unread_count'] = unread
|
||||||
|
Loading…
Reference in New Issue
Block a user