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

Show low-level OS error on TCP connection errors. #264

Merged
merged 1 commit into from
Jan 27, 2024
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
2 changes: 1 addition & 1 deletion src/smbprotocol/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def connect(self):
try:
self._sock = socket.create_connection((self.server, self.port), timeout=self.timeout)
except (OSError, socket.gaierror) as err:
raise ValueError(f"Failed to connect to '{self.server}:{self.port}'") from err
raise ValueError(f"Failed to connect to '{self.server}:{self.port}': {err}") from err
self._sock.settimeout(None) # Make sure the socket is in blocking mode.
self.connected = True

Expand Down
9 changes: 8 additions & 1 deletion tests/test_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,15 @@ def test_normal_fail_message_too_big(self):
)

def test_invalid_host(self):
"""
Raises ValueError when failing to connect to the remote server.

The error message contains the low-level OS error details.
"""
tcp = Tcp("fake-host", 445)
with pytest.raises(ValueError, match=re.escape("Failed to connect to 'fake-host:445'")):
# We just check for OSError marker, as the actual error message
# might be different based on current OS.
with pytest.raises(ValueError, match=r"Failed to connect to 'fake-host:445': \[Errno .*"):
tcp.connect()


Expand Down
Loading