Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt tests to new urllib exception #192

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
16 changes: 16 additions & 0 deletions linkcheck/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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\([\"']<urllib3\.connection\.HTTPSConnection object at 0x[0-9a-f]+>: (.+)[\"']\)",
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
Expand Down
2 changes: 1 addition & 1 deletion linkcheck/tests/test_linkcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading