Skip to content

Commit

Permalink
fix: preserve errno if all exceptions have the same errno
Browse files Browse the repository at this point in the history
fixes #76
  • Loading branch information
bdraco committed Aug 7, 2024
1 parent ef94449 commit af2bc91
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/aiohappyeyeballs/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,29 @@ async def start_connection(
if sock is None:
all_exceptions = [exc for sub in exceptions for exc in sub]
try:
first_exception = all_exceptions[0]
if len(all_exceptions) == 1:
raise all_exceptions[0]
raise first_exception
else:
# If they all have the same str(), raise one.
model = str(all_exceptions[0])
model = str(first_exception)
if all(str(exc) == model for exc in all_exceptions):
raise all_exceptions[0]
raise first_exception
# Raise a combined exception so the user can see all
# the various error messages.
raise OSError(
"Multiple exceptions: {}".format(
", ".join(str(exc) for exc in all_exceptions)
)
msg = "Multiple exceptions: {}".format(
", ".join(str(exc) for exc in all_exceptions)
)
# If the errno is the same for all exceptions, raise
# an OSError with that errno.
if isinstance(first_exception, OSError):
first_errno = first_exception.errno
if all(
isinstance(exc, OSError) and exc.errno == first_errno
for exc in all_exceptions
):
raise OSError(first_errno, msg)
raise OSError(msg)

Check warning on line 124 in src/aiohappyeyeballs/impl.py

View check run for this annotation

Codecov / codecov/patch

src/aiohappyeyeballs/impl.py#L124

Added line #L124 was not covered by tests
finally:
all_exceptions = None # type: ignore[assignment]
exceptions = None # type: ignore[assignment]
Expand Down

0 comments on commit af2bc91

Please sign in to comment.