magic-proxy: fix exception handling for URLError LP:#1946520

A urllib.error.URLError.reason variable can either be a string or
another Exception[0]. In case it's another exception, the current code
fails because the exception is passed into send_error() which tries
call html.escape() on the Exception. That fails because the Exception
is not a string. Converting the Exception to a string fixes this.

This fixes:

AttributeError: 'TimeoutError' object has no attribute 'replace'

[0]
https://docs.python.org/3/library/urllib.error.html#urllib.error.URLError.reason

(cherry picked from commit af888e24ff)
impish-sru-lp-1946520
Thomas Bechtold 3 years ago
parent 5339f1691b
commit 408045732e

@ -883,8 +883,10 @@ class ProxyingHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
self.__send_response(e)
except urllib.error.URLError as e:
self.log_message(
"urlopen() failed for {} with {}".format(uri, e.reason))
self.send_error(501, e.reason)
"urlopen() failed for {} with {}".format(uri, str(e.reason)))
# URLError.reason can either be a string or another Exception
# So do convert it to a string before sending the error (LP: #1946520)
self.send_error(501, str(e.reason))
def __get_host_path(self):

Loading…
Cancel
Save