From 15f8a7adfd2fda2c75766075db28aef155f073c4 Mon Sep 17 00:00:00 2001 From: Timo Brembeck Date: Wed, 15 Nov 2023 14:01:33 +0100 Subject: [PATCH] Adapt tests to new urllib exception --- CHANGELOG | 1 + linkcheck/models.py | 16 ++++++++++++++++ linkcheck/tests/test_linkcheck.py | 2 +- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index f0ea691..76aa89f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ Unreleased +* Improve formatting for `NameResolutionError` (Timo Brembeck, #192) * Fix internal redirect checker (Timo Ludwig, #180) * Fix SSL status of unreachable domains (Timo Ludwig, #184) * Fix URL message for internal server errorrs (Timo Ludwig, #182) diff --git a/linkcheck/models.py b/linkcheck/models.py index bb651a2..50c963a 100644 --- a/linkcheck/models.py +++ b/linkcheck/models.py @@ -567,6 +567,9 @@ def format_connection_error(e): # If the underlying cause is a new connection error, provide additional formatting if reason.startswith("NewConnectionError"): return format_new_connection_error(reason) + # If the underlying cause is a name resolution error, provide additional formatting + if reason.startswith("NameResolutionError"): + return format_name_resolution_error(reason) # If the underlying cause is an SSL error, provide additional formatting if reason.startswith("SSLError"): return format_ssl_error(reason) @@ -586,6 +589,19 @@ def format_new_connection_error(reason): return reason +def format_name_resolution_error(reason): + """ + Helper function to provide better readable output of name resolution errors thrown by urllib3 + """ + resolution_reason = re.search( + r"NameResolutionError\([\"']: (.+)[\"']\)", + reason, + ) + if resolution_reason: + return f"Name Resolution Error: {resolution_reason[1]}" + return reason + + def format_ssl_error(reason): """ Helper function to provide better readable output of SSL errors thrown by urllib3 diff --git a/linkcheck/tests/test_linkcheck.py b/linkcheck/tests/test_linkcheck.py index a857efb..e915166 100644 --- a/linkcheck/tests/test_linkcheck.py +++ b/linkcheck/tests/test_linkcheck.py @@ -443,7 +443,7 @@ def test_external_check_unreachable(self): for attr in [uv.message, uv.get_message, uv.error_message]: self.assertEqual( attr, - 'New Connection Error: Failed to establish a new connection: [Errno -2] Name or service not known', + "Name Resolution Error: Failed to resolve 'invalid' ([Errno -2] Name or service not known)", ) self.assertEqual(uv.anchor_message, '') self.assertEqual(uv.ssl_status, None)