diff options
author | James Taylor <user234683@users.noreply.github.com> | 2021-07-03 21:31:45 -0700 |
---|---|---|
committer | Jesús <heckyel@hyperbola.info> | 2021-07-04 13:20:48 -0500 |
commit | aadc20fa1925018e2c1b72ca89ad7f7f123ea74b (patch) | |
tree | df40f3af9e9444b5fc8c9db8bbd3a7ef63345aed | |
parent | 7f67af10315f89d196c7380e0f2cbbb8c7b5aa5e (diff) | |
download | yt-local-aadc20fa1925018e2c1b72ca89ad7f7f123ea74b.tar.lz yt-local-aadc20fa1925018e2c1b72ca89ad7f7f123ea74b.tar.xz yt-local-aadc20fa1925018e2c1b72ca89ad7f7f123ea74b.zip |
Friendlier error message when Tor is closed or network is down
Signed-off-by: Jesús <heckyel@hyperbola.info>
-rw-r--r-- | youtube/__init__.py | 6 | ||||
-rw-r--r-- | youtube/util.py | 25 |
2 files changed, 28 insertions, 3 deletions
diff --git a/youtube/__init__.py b/youtube/__init__.py index 9a5f66c..08f4f6f 100644 --- a/youtube/__init__.py +++ b/youtube/__init__.py @@ -108,6 +108,12 @@ def error_page(e): if exc_info()[1].ip: error_message += '\n\nExit node IP address: ' + exc_info()[1].ip return flask.render_template('error.html', error_message=error_message, slim=slim), 502 + elif exc_info()[0] == util.FetchError and exc_info()[1].error_message: + return (flask.render_template( + 'error.html', + error_message=exc_info()[1].error_message, + slim=slim + ), 502) return flask.render_template('error.html', traceback=traceback.format_exc(), slim=slim), 500 diff --git a/youtube/util.py b/youtube/util.py index 18c7ca1..ca4faa2 100644 --- a/youtube/util.py +++ b/youtube/util.py @@ -240,6 +240,7 @@ def fetch_url_response(url, headers=(), timeout=15, data=None, elif not isinstance(data, bytes): data = urllib.parse.urlencode(data).encode('utf-8') + if cookiejar_send is not None or cookiejar_receive is not None: # Use urllib req = urllib.request.Request(url, data=data, headers=headers) @@ -263,9 +264,27 @@ def fetch_url_response(url, headers=(), timeout=15, data=None, else: retries = urllib3.Retry(3) pool = get_pool(use_tor and settings.route_tor) - response = pool.request(method, url, headers=headers, body=data, - timeout=timeout, preload_content=False, - decode_content=False, retries=retries) + try: + response = pool.request(method, url, headers=headers, body=data, + timeout=timeout, preload_content=False, + decode_content=False, retries=retries) + except urllib3.exceptions.MaxRetryError as e: + exception_cause = e.__context__.__context__ + if (isinstance(exception_cause, socks.ProxyConnectionError) + and settings.route_tor): + msg = ('Failed to connect to Tor. Check that Tor is open and ' + 'that your internet connection is working.\n\n' + + str(e)) + raise FetchError('502', reason='Bad Gateway', + error_message=msg) + elif isinstance(e.__context__, + urllib3.exceptions.NewConnectionError): + msg = 'Failed to establish a connection.\n\n' + str(e) + raise FetchError( + '502', reason='Bad Gateway', + error_message=msg) + else: + raise cleanup_func = (lambda r: r.release_conn()) return response, cleanup_func |