From 28cc5cbe69924373300edf7d9ea8c006aa4a7ec5 Mon Sep 17 00:00:00 2001 From: Thomas Bechtold Date: Thu, 21 Oct 2021 10:54:10 +0200 Subject: [PATCH] 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 af888e24ff8ec478b490c6d0fc39131cd63a8079) --- magic-proxy | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/magic-proxy b/magic-proxy index 10f19c47..af765c32 100755 --- a/magic-proxy +++ b/magic-proxy @@ -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):