From 77e57da470d337efe599aed96e432dd27e5f7891 Mon Sep 17 00:00:00 2001 From: hvelarde Date: Thu, 28 Sep 2017 02:45:36 -0300 Subject: [PATCH] Avoid failures on requests exceptions --- sc/social/like/subscribers.py | 6 +++++- sc/social/like/tests/test_subscribers.py | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/sc/social/like/subscribers.py b/sc/social/like/subscribers.py index 5f2df656..3a21dea7 100644 --- a/sc/social/like/subscribers.py +++ b/sc/social/like/subscribers.py @@ -172,7 +172,11 @@ def prefetch_facebook(obj, event): url = obj.absolute_url() endpoint = 'https://graph.facebook.com/?id=' + url + '&scrape=true' - r = requests.post(endpoint, timeout=5) + try: + r = requests.post(endpoint, timeout=5) + except requests.exceptions.RequestException as e: + logger.warn('Prefetch failure: ' + str(e.message)) + return if r.status_code == '200': logger.info('Prefetch successful: ' + url) diff --git a/sc/social/like/tests/test_subscribers.py b/sc/social/like/tests/test_subscribers.py index 7c5eb483..66bdbd89 100644 --- a/sc/social/like/tests/test_subscribers.py +++ b/sc/social/like/tests/test_subscribers.py @@ -278,6 +278,17 @@ def test_facebook_prefetch_error(self, m, l): expected = ('sc.social.like', 'WARNING', msg) l.check(expected) + @requests_mock.mock() + def test_facebook_prefetch_failure(self, m): + import requests + m.post(self.endpoint, exc=requests.exceptions.ConnectTimeout) + + with api.env.adopt_roles(['Manager']): + api.content.transition(self.news_item, 'publish') + + # transition should not be aborted + self.assertEqual(api.content.get_state(self.news_item), 'published') + def load_tests(loader, tests, pattern): from sc.social.like.testing import HAS_DEXTERITY