From aac00356a1fb58342c653c136ef4fb82336b1569 Mon Sep 17 00:00:00 2001 From: kiskaadee Date: Thu, 19 Nov 2026 01:59:34 -0500 Subject: [PATCH] fix(capture): map asyncssh.KeyImportError to SCREENSHOT_UNAVAILABLE - src/lg_smtv_mcp/tv_core/capture.py: catch asyncssh.KeyImportError (which subclasses ValueError rather than asyncssh.Error) in AsyncSshCaptureTransport.read_and_remove() and CaptureClient.capture_frame() so encrypted/invalid SSH key errors surface as TvError(SCREENSHOT_UNAVAILABLE) and degrade gracefully on composite capture calls - tests/test_capture.py: add test_capture_client_maps_key_import_error_to_screenshot_unavailable --- src/lg_smtv_mcp/tv_core/capture.py | 4 ++-- tests/test_capture.py | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/lg_smtv_mcp/tv_core/capture.py b/src/lg_smtv_mcp/tv_core/capture.py index 10ba458..a5f0301 100644 --- a/src/lg_smtv_mcp/tv_core/capture.py +++ b/src/lg_smtv_mcp/tv_core/capture.py @@ -105,7 +105,7 @@ class AsyncSshCaptureTransport: async with sftp.open(remote_path, "rb") as handle: data = await handle.read() return bytes(data) - except asyncssh.Error, OSError: + except asyncssh.Error, asyncssh.KeyImportError, OSError: await self._close_handles() raise finally: @@ -192,7 +192,7 @@ class CaptureClient: ) except TvError: raise - except (asyncssh.Error, OSError) as exc: + except (asyncssh.Error, asyncssh.KeyImportError, OSError) as exc: raise TvError( ErrorCode.SCREENSHOT_UNAVAILABLE, f"Failed to retrieve screenshot over SSH/SFTP: {exc}", diff --git a/tests/test_capture.py b/tests/test_capture.py index f36ec06..145ce81 100644 --- a/tests/test_capture.py +++ b/tests/test_capture.py @@ -504,3 +504,21 @@ async def test_capture_client_maps_sftp_timeout_and_cancels_read() -> None: assert exc_info.value.code is ErrorCode.SCREENSHOT_UNAVAILABLE assert transport.read_cancelled.is_set() + + +async def test_capture_client_maps_key_import_error_to_screenshot_unavailable() -> None: + transport = FakeCaptureTransport( + read_error=asyncssh.KeyImportError( + "Passphrase must be specified to import encrypted private keys" + ) + ) + client = CaptureClient(transport) + + async def ssap_request(uri: str, payload: dict[str, Any]) -> dict[str, Any]: + _ = (uri, payload) + return {"returnValue": True} + + with pytest.raises(TvError) as exc_info: + await client.capture_frame(ssap_request) + assert exc_info.value.code is ErrorCode.SCREENSHOT_UNAVAILABLE + assert "Passphrase must be specified" in exc_info.value.message