From 6cf07663174c10d5416398a0f88e4734760ad9d6 Mon Sep 17 00:00:00 2001
From: Fernando <fernandog@users.noreply.github.com>
Date: Wed, 15 Jul 2015 08:49:19 -0300
Subject: [PATCH] Fix trakt exceptions when connection timed out

Avoid this:
```
AA
AAtraktException: HTTPSConnectionPool(host='api-v2launch.trakt.tv', port=443): Read timed out. (read timeout=5)
AA    raise traktException(e)
AA  File "/home/osmc/SickRage/lib/trakt/trakt.py", line 97, in traktRequest
AA    TraktEpisodeWatchlist = self.trakt_api.traktRequest("sync/watchlist/episodes")
AA  File "/home/osmc/SickRage/sickbeard/traktChecker.py", line 503, in _getEpisodeWatchlist
AA    if self._getEpisodeWatchlist():
AA  File "/home/osmc/SickRage/sickbeard/traktChecker.py", line 237, in syncWatchlist
AA    self.syncWatchlist()
AA  File "/home/osmc/SickRage/sickbeard/traktChecker.py", line 82, in run
```
---
 lib/trakt/trakt.py | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/lib/trakt/trakt.py b/lib/trakt/trakt.py
index f8f048026..c5293ee32 100644
--- a/lib/trakt/trakt.py
+++ b/lib/trakt/trakt.py
@@ -92,18 +92,21 @@ class TraktAPI():
         except requests.RequestException as e:
             code = getattr(e.response, 'status_code', None)
             if not code:
+                if 'timed out' in e:
+                    logger.log(u'Timeout connecting to Trakt. Try to increase timeout value in Trakt settings', logger.WARNING)                      
                 # This is pretty much a fatal error if there is no status_code
-                # It means there basically was no response at all
-                raise traktException(e)
+                # It means there basically was no response at all                    
+                else:
+                    logger.log(u'Could not connect to Trakt. Error: {0}'.format(e), logger.WARNING)                
             elif code is 502:
                 # Retry the request, cloudflare had a proxying issue
                 logger.log(u'Retrying trakt api request: %s' % path, logger.WARNING)
                 return self.traktRequest(path, data, headers, url, method)
             elif code is 401:
-                logger.log(u'Unauthorized. Please check your Trakt settings', logger.WARNING)
                 if self.traktToken(refresh=True, count=count):
                     return self.traktRequest(path, data, headers, url, method)
-                raise traktAuthException(e)
+                else:
+                    logger.log(u'Unauthorized. Please check your Trakt settings', logger.WARNING)
             elif code in (500,501,503,504,520,521,522):
                 #http://docs.trakt.apiary.io/#introduction/status-codes
                 logger.log(u'Trakt may have some issues and it\'s unavailable. Try again later please', logger.WARNING)
@@ -112,7 +115,8 @@ class TraktAPI():
                 logger.log(u'Trakt error (404) the resource does not exist: %s' % url + path, logger.WARNING)
                 return {}
             else:
-                raise traktException(e)
+                logger.log(u'Could not connect to Trakt. Code error: {0}'.format(code), logger.ERROR)
+                return {}
 
         # check and confirm trakt call did not fail
         if isinstance(resp, dict) and resp.get('status', False) == 'failure':
-- 
GitLab