Skip to content

Commit 905ab74

Browse files
timobrembeckclaudep
authored andcommitted
Adapt tests to new urllib exception
1 parent 3d6188e commit 905ab74

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Unreleased
22

3+
* Improve formatting for `NameResolutionError` (Timo Brembeck, #192)
34
* Fix internal redirect checker (Timo Ludwig, #180)
45
* Fix SSL status of unreachable domains (Timo Ludwig, #184)
56
* Fix URL message for internal server errorrs (Timo Ludwig, #182)

linkcheck/models.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,9 @@ def format_connection_error(e):
567567
# If the underlying cause is a new connection error, provide additional formatting
568568
if reason.startswith("NewConnectionError"):
569569
return format_new_connection_error(reason)
570+
# If the underlying cause is a name resolution error, provide additional formatting
571+
if reason.startswith("NameResolutionError"):
572+
return format_name_resolution_error(reason)
570573
# If the underlying cause is an SSL error, provide additional formatting
571574
if reason.startswith("SSLError"):
572575
return format_ssl_error(reason)
@@ -586,6 +589,19 @@ def format_new_connection_error(reason):
586589
return reason
587590

588591

592+
def format_name_resolution_error(reason):
593+
"""
594+
Helper function to provide better readable output of name resolution errors thrown by urllib3
595+
"""
596+
resolution_reason = re.search(
597+
r"NameResolutionError\([\"']<urllib3\.connection\.HTTPSConnection object at 0x[0-9a-f]+>: (.+)[\"']\)",
598+
reason,
599+
)
600+
if resolution_reason:
601+
return f"Name Resolution Error: {resolution_reason[1]}"
602+
return reason
603+
604+
589605
def format_ssl_error(reason):
590606
"""
591607
Helper function to provide better readable output of SSL errors thrown by urllib3

linkcheck/tests/test_linkcheck.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ def test_external_check_unreachable(self):
443443
for attr in [uv.message, uv.get_message, uv.error_message]:
444444
self.assertEqual(
445445
attr,
446-
'New Connection Error: Failed to establish a new connection: [Errno -2] Name or service not known',
446+
"Name Resolution Error: Failed to resolve 'invalid' ([Errno -2] Name or service not known)",
447447
)
448448
self.assertEqual(uv.anchor_message, '')
449449
self.assertEqual(uv.ssl_status, None)

0 commit comments

Comments
 (0)