Fix wrapping native RequiresEncryption error types (#113)

This commit is contained in:
Otto Winter 2021-10-05 10:56:35 +02:00 committed by GitHub
parent e422847861
commit 06806b4490
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 5 deletions

View File

@ -584,9 +584,10 @@ class APIConnection:
def on_read_exception(exc: Exception) -> None:
if not fut.done():
# Wrap error so that caller gets right stacktrace
new_exc = ReadFailedAPIError("Read failed")
new_exc.__cause__ = exc
new_exc = exc
if not isinstance(exc, APIConnectionError):
new_exc = ReadFailedAPIError("Read failed")
new_exc.__cause__ = exc
fut.set_exception(new_exc)
self._message_handlers.append(on_message)

View File

@ -5,8 +5,8 @@ import pytest
from mock import AsyncMock, MagicMock, Mock, patch
from aioesphomeapi.api_pb2 import ConnectResponse, HelloResponse
from aioesphomeapi.connection import APIConnection, ConnectionParams
from aioesphomeapi.core import APIConnectionError
from aioesphomeapi.connection import APIConnection, ConnectionParams, ConnectionState
from aioesphomeapi.core import APIConnectionError, RequiresEncryptionAPIError
from aioesphomeapi.host_resolver import AddrInfo, IPv4Sockaddr
@ -62,3 +62,18 @@ async def test_connect(conn, resolve_host, socket_socket, event_loop):
await conn.connect()
assert conn.is_connected
@pytest.mark.asyncio
async def test_requires_encryption_propagates(conn):
with patch("asyncio.open_connection") as openc:
reader = MagicMock()
writer = MagicMock()
openc.return_value = (reader, writer)
writer.drain = AsyncMock()
reader.readexactly = AsyncMock()
reader.readexactly.return_value = b"\x01"
await conn._connect_init_frame_helper()
with pytest.raises(RequiresEncryptionAPIError):
await conn._connect_hello()