Skip to content

Commit

Permalink
Attempt to provide sensible errors on network issues
Browse files Browse the repository at this point in the history
Currently the add-on works fine if all is well, however various things
can go wrong which makes the add-on bail out with no proper error.

And since the cause of network errors could be anywhere, we do not want
the end-user to have to dig deep into Kodi logs to find probable
suspects.

So we do want to return the exact error message (i.e. Connection
refused, No route to host, etc.) so the user can relate it to other
possible problems. (WIFI issues, Internet down or unreliable, etc.)
  • Loading branch information
dagwieers committed Jul 15, 2019
1 parent fcdd04d commit 68c33f7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 17 deletions.
8 changes: 8 additions & 0 deletions resources/language/resource.language.en_gb/strings.po
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,14 @@ msgctxt "#30962"
msgid "There is a problem with this VRT NU {protocol} stream and Kodi {version} does not support the alternative stream. Please upgrade to a newer Kodi version or try to play this program from the VRT NU website. Please report this problem at https://www.vrt.be/vrtnu/help/"
msgstr ""

msgctxt "#30963"
msgid "Unable to open URL"
msgstr ""

msgctxt "#30964"
msgid "There is a problem connecting to the Internet. This could be related to Kodi, your network, your ISP or VRT NU.\n\nError: {error}\nURL: {url}"
msgstr ""

msgctxt "#30965"
msgid "Using a SOCKS proxy requires the PySocks library (script.module.pysocks) installed."
msgstr ""
Expand Down
8 changes: 8 additions & 0 deletions resources/language/resource.language.nl_nl/strings.po
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,14 @@ msgctxt "#30962"
msgid "There is a problem with this VRT NU {protocol} stream and Kodi {version} does not support the alternative stream. Please upgrade to a newer Kodi version or try to play this program from the VRT NU website. Please report this problem at https://www.vrt.be/vrtnu/help/"
msgstr "Er is een probleem met deze VRT NU {protocol}-stream en Kodi {version} ondersteunt de alternatieve stream niet. Schakel over op een nieuwere versie van Kodi of probeer dit programma af te spelen vanaf de VRT NU-website. Meld dit probleem op https://www.vrt.be/vrtnu/help/"

msgctxt "#30963"
msgid "Unable to open URL"
msgstr "Probleem bij het openen van een URL"

msgctxt "#30964"
msgid "There is a problem connecting to the Internet. This could be related to Kodi, your network, your ISP or VRT NU.\n\nError: {error}\nURL: {url}"
msgstr "Er is een fout opgetreden met het Internet gebruik. Dit kan liggen aan Kodi, aan jouw netwerk, aan jouw ISP of aan VRT NU.\n\nFout: {error}\nURL: {url}"

msgctxt "#30965"
msgid "Using a SOCKS proxy requires the PySocks library (script.module.pysocks) installed."
msgstr "Het gebruik van SOCKS proxies vereist dat de PySocks library (script.module.pysocks) geïnstalleerd is."
Expand Down
59 changes: 42 additions & 17 deletions resources/lib/tokenresolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@

try: # Python 3
import http.cookiejar as cookielib
from urllib.parse import urlencode, unquote
from urllib.request import build_opener, install_opener, ProxyHandler, HTTPCookieProcessor, HTTPErrorProcessor, urlopen, Request
from urllib.error import URLError
from urllib.parse import unquote, urlencode
from urllib.request import HTTPCookieProcessor, HTTPErrorProcessor, ProxyHandler, Request, build_opener, install_opener, urlopen
except ImportError: # Python 2
from urllib import urlencode
from urllib2 import build_opener, install_opener, ProxyHandler, HTTPCookieProcessor, HTTPErrorProcessor, unquote, urlopen, Request
from urllib2 import HTTPCookieProcessor, HTTPErrorProcessor, ProxyHandler, Request, URLError, build_opener, install_opener, unquote, urlopen
import cookielib


Expand Down Expand Up @@ -231,9 +232,17 @@ def _get_new_user_xvrttoken(self):
cookiejar = cookielib.CookieJar()
opener = build_opener(HTTPCookieProcessor(cookiejar), ProxyHandler(self._proxies))
self._kodi.log_notice('URL get: ' + unquote(self._USER_TOKEN_GATEWAY_URL), 'Verbose')
opener.open(self._USER_TOKEN_GATEWAY_URL)
try:
opener.open(self._USER_TOKEN_GATEWAY_URL)
except URLError as e:
self._kodi.show_ok_dialog(heading=self._kodi.localize(30963), message=self._kodi.localize(30964, error=e, url=unquote(self._USER_TOKEN_GATEWAY_URL)))
raise
self._kodi.log_notice('URL post: ' + unquote(self._VRT_LOGIN_URL), 'Verbose')
opener.open(self._VRT_LOGIN_URL, data=data)
try:
opener.open(self._VRT_LOGIN_URL, data=data)
except URLError as e:
self._kodi.show_ok_dialog(heading=self._kodi.localize(30963), message=self._kodi.localize(30964, error=e, url=unquote(self._VRT_LOGIN_URL)))
raise
xvrttoken = TokenResolver._create_token_dictionary(cookiejar)
refreshtoken = TokenResolver._create_token_dictionary(cookiejar, cookie_name='vrtlogin-rt')
if xvrttoken is not None:
Expand All @@ -253,7 +262,11 @@ def _get_fresh_token(self, refresh_token, token_name, token_variant=None):
opener = build_opener(HTTPCookieProcessor(cookiejar), ProxyHandler(self._proxies))
self._kodi.log_notice('URL get: ' + refresh_url, 'Verbose')
req = Request(refresh_url, headers=headers)
opener.open(req)
try:
opener.open(req)
except URLError as e:
self._kodi.show_ok_dialog(heading=self._kodi.localize(30963), message=self._kodi.localize(30964, error=e, url=req.full_url))
raise
token = TokenResolver._create_token_dictionary(cookiejar, token_name)
self._set_cached_token(token, token_variant)
return list(token.values())[0]
Expand All @@ -273,34 +286,46 @@ def _handle_login_error(self, logon_json, cred):

def _get_roaming_xvrttoken(self, xvrttoken):
''' Get new 'roaming' X-VRT-Token from VRT NU website '''
roaming_xvrttoken = None
cookie_value = 'X-VRT-Token=' + xvrttoken.get('X-VRT-Token')
headers = {'Cookie': cookie_value}
opener = build_opener(NoRedirection, ProxyHandler(self._proxies))
self._kodi.log_notice('URL get: ' + unquote(self._ROAMING_TOKEN_GATEWAY_URL), 'Verbose')
req = Request(self._ROAMING_TOKEN_GATEWAY_URL, headers=headers)
req_info = opener.open(req).info()
try:
req_info = opener.open(req).info()
except URLError as e:
self._kodi.show_ok_dialog(heading=self._kodi.localize(30963), message=self._kodi.localize(30964, error=e, url=req.full_url))
raise
try: # Python 3
cookie_value += '; state=' + req_info.get('Set-Cookie').split('state=')[1].split('; ')[0]
url = req_info.get('Location')
except AttributeError: # Python 2
cookie_value += '; state=' + req_info.getheader('Set-Cookie').split('state=')[1].split('; ')[0]
url = req_info.getheader('Location')
self._kodi.log_notice('URL get: ' + unquote(url), 'Verbose')
try: # Python 3
url = opener.open(url).info().get('Location')
except AttributeError: # Python 2
url = opener.open(url).info().getheader('Location')
try:
try: # Python 3
url = opener.open(url).info().get('Location')
except AttributeError: # Python 2
url = opener.open(url).info().getheader('Location')
except URLError as e:
self._kodi.show_ok_dialog(heading=self._kodi.localize(30963), message=self._kodi.localize(30964, error=e, url=url))
raise
if url is None:
return None

headers = {'Cookie': cookie_value}
if url is not None:
self._kodi.log_notice('URL get: ' + unquote(url), 'Verbose')
req = Request(url, headers=headers)
self._kodi.log_notice('URL get: ' + unquote(url), 'Verbose')
req = Request(url, headers=headers)
try:
try: # Python 3
setcookie_header = opener.open(req).info().get('Set-Cookie')
except AttributeError: # Python 2
setcookie_header = opener.open(req).info().getheader('Set-Cookie')
roaming_xvrttoken = TokenResolver._create_token_dictionary(setcookie_header)
return roaming_xvrttoken
except URLError as e:
self._kodi.show_ok_dialog(heading=self._kodi.localize(30963), message=self._kodi.localize(30964, error=e, url=req.full_url))
raise
return TokenResolver._create_token_dictionary(setcookie_header)

@staticmethod
def _create_token_dictionary(cookie_data, cookie_name='X-VRT-Token'):
Expand Down

0 comments on commit 68c33f7

Please sign in to comment.