From 1c87b6faa738873daecd08033b94eae830b973d2 Mon Sep 17 00:00:00 2001 From: Aurélien Bompard Date: Feb 26 2018 18:24:53 +0000 Subject: Protect widgets against unavailable backend server --- diff --git a/hubs/tests/widgets/test_badges.py b/hubs/tests/widgets/test_badges.py index c8b2a6d..647d6c5 100644 --- a/hubs/tests/widgets/test_badges.py +++ b/hubs/tests/widgets/test_badges.py @@ -72,7 +72,7 @@ class TestBadges(WidgetTest): widget = widget_instance('ralph', self.plugin) func = widget.module.get_cached_functions()['GetBadges'] result = func(widget).execute() - self.assertEqual(result, {}) + self.assertEqual(result, None) @patch("hubs.widgets.badges.requests") def test_malformed_data(self, requests): @@ -82,4 +82,4 @@ class TestBadges(WidgetTest): widget = widget_instance('ralph', self.plugin) func = widget.module.get_cached_functions()['GetBadges'] result = func(widget).execute() - self.assertEqual(result, {}) + self.assertEqual(result, None) diff --git a/hubs/utils/datagrepper.py b/hubs/utils/datagrepper.py index 0a266aa..adc37e0 100644 --- a/hubs/utils/datagrepper.py +++ b/hubs/utils/datagrepper.py @@ -36,7 +36,7 @@ def datagrepper_get(args): log.warning( "Datagrepper request timed out (args: %s)", paged_args) return None - if response.status_code != 200: + if not response.ok: log.warning( "Datagrepper request failed with args %s: %s", paged_args, response.text) diff --git a/hubs/utils/github.py b/hubs/utils/github.py index 148efa6..0b374c9 100644 --- a/hubs/utils/github.py +++ b/hubs/utils/github.py @@ -21,7 +21,10 @@ def github_repo_is_valid(username, repo): log.info("Finding github repo for {} and {} ".format(repo, username)) tmpl = "https://api.github.com/repos/{username}/{repo}" url = tmpl.format(username=username, repo=repo) - result = requests.get(url, timeout=5) + try: + result = requests.get(url, timeout=5) + except requests.exceptions.RequestException: + return False return result.ok diff --git a/hubs/widgets/badges/__init__.py b/hubs/widgets/badges/__init__.py index 344048b..7f6c0d5 100644 --- a/hubs/widgets/badges/__init__.py +++ b/hubs/widgets/badges/__init__.py @@ -1,5 +1,7 @@ from __future__ import unicode_literals +import logging + import requests from collections import defaultdict, OrderedDict @@ -8,6 +10,9 @@ from hubs.widgets.view import RootWidgetView from hubs.widgets.caching import CachedFunction +log = logging.getLogger('hubs.widgets') + + class Badges(Widget): name = "badges" position = "right" @@ -24,7 +29,13 @@ class BaseView(RootWidgetView): def get_context(self, instance, *args, **kwargs): context = {} get_badges = GetBadges(instance) - context.update(get_badges()) + badges = get_badges() + if badges is None: + return dict( + failure="Could not connect to the badges server, please try " + "again later." + ) + context.update(badges) return context @@ -34,12 +45,21 @@ class GetBadges(CachedFunction): username = self.instance.hub.name url = "https://badges.fedoraproject.org/user/{username}/json" url = url.format(username=username) - response = requests.get(url) + try: + response = requests.get(url) + except requests.exceptions.RequestException as e: + log.warning( + "Badges widget: could not get URL {}: {}".format(url, e), + exc_info=True) + return None try: response = response.json() assertions = response['assertions'] - except (KeyError, ValueError): - return {} + except (KeyError, ValueError) as e: + log.warning( + "Badges widget: could not read response from URL " + "{}: {}".format(url, e), exc_info=True) + return None tag_totals = defaultdict(int) latest_badge = None for badge in assertions: @@ -67,6 +87,9 @@ class GetBadges(CachedFunction): badge_percentage=round(response["percent_earned"], 1) ) + def _should_cache(self, value): + return value is not None + def should_invalidate(self, message): if message['topic'].endswith('hubs.widget.update'): if message['msg']['widget']['id'] != self.instance.idx: diff --git a/hubs/widgets/badges/templates/root.html b/hubs/widgets/badges/templates/root.html index 94d5e34..d381718 100644 --- a/hubs/widgets/badges/templates/root.html +++ b/hubs/widgets/badges/templates/root.html @@ -1,3 +1,8 @@ +{% if failure %} +

+ {{ failure }} +

+{% else %}
Badges Earned
@@ -39,6 +44,7 @@
+{% endif %}