From f787e4e2027583476ca34bd01c8462f6459369bb Mon Sep 17 00:00:00 2001 From: James Taylor Date: Fri, 31 Jan 2020 20:06:15 -0800 Subject: Give a proper error message for 429 errors These occur when too many requests are coming from a Tor exit node. Before, there would be an error page with an exception instructing users to report the issue. But this is an expected and persistent issue. --- youtube/__init__.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'youtube/__init__.py') diff --git a/youtube/__init__.py b/youtube/__init__.py index d8171c0..9e95256 100644 --- a/youtube/__init__.py +++ b/youtube/__init__.py @@ -1,6 +1,8 @@ +from youtube import util import flask import settings import traceback +from sys import exc_info yt_app = flask.Flask(__name__) yt_app.url_map.strict_slashes = False @@ -34,4 +36,14 @@ def commatize(num): @yt_app.errorhandler(500) def error_page(e): + if (exc_info()[0] == util.FetchError + and exc_info()[1].code == '429' + and settings.route_tor + ): + error_message = ('Error: Youtube blocked the request because the Tor' + ' exit node is overcrowded. Try getting a new exit node by' + ' restarting the Tor Browser.') + if exc_info()[1].ip: + error_message += ' Exit node IP address: ' + exc_info()[1].ip + return flask.render_template('error.html', error_message=error_message), 502 return flask.render_template('error.html', traceback=traceback.format_exc()), 500 -- cgit v1.2.3