diff --git a/src/labone/core/_error_handlers/__init__.py b/src/labone/core/_error_handlers/__init__.py deleted file mode 100644 index 2543ea6..0000000 --- a/src/labone/core/_error_handlers/__init__.py +++ /dev/null @@ -1,10 +0,0 @@ -"""Subpackage for handling errors. - -The package provides helper functionality to convert errors that -happens while assembling a request or after receiving a successful -response, which has an error status. -""" - -from labone.core._error_handlers.schema_errors import convert_dynamic_schema_error - -__all__ = ["convert_dynamic_schema_error"] diff --git a/src/labone/core/_error_handlers/schema_errors.py b/src/labone/core/_error_handlers/schema_errors.py deleted file mode 100644 index d99d0c3..0000000 --- a/src/labone/core/_error_handlers/schema_errors.py +++ /dev/null @@ -1,31 +0,0 @@ -"""Error handler module for capnp schema errors. - -This module provides helper functionality to convert errors -raised from invalid schema values. -""" -import capnp - -from labone.core import errors - - -def convert_dynamic_schema_error(exception: Exception, msg_prefix: str = "") -> None: - """Generic converter for dynamic `capnp` schema errors. - - Converts exceptions raised while assembling a schema into library errors. - - Args: - exception: Exception occured while building the request. - msg_prefix: Prefix for the exception message. - - Raises: - LabOneCoreError: All errors are translated to `LabOneCoreError`. - """ - # TODO(markush): # noqa: FIX002, TD003 - # Format error messages to an understandable format. - # Will probably require to take in the original request and field name - # to access the schema. - if isinstance(exception, capnp.lib.capnp.KjException): - msg = msg_prefix + exception.description - raise errors.LabOneCoreError(msg) from exception - msg = msg_prefix + str(exception) - raise errors.LabOneCoreError(msg) from exception diff --git a/src/labone/core/session.py b/src/labone/core/session.py index d2cca33..05b5cc2 100644 --- a/src/labone/core/session.py +++ b/src/labone/core/session.py @@ -9,7 +9,6 @@ from typing_extensions import Literal, NotRequired, TypeAlias, TypedDict from labone.core import errors -from labone.core._error_handlers import convert_dynamic_schema_error from labone.core.connection_layer import ( KernelInfo, ServerInfo, @@ -126,11 +125,6 @@ class ListNodesFlags(IntFlag): EXCLUDE_VECTORS = 1 << 24 -def _argument_error_msg_prefix(arg: str) -> str: - """Error message prefix for conveying from which argument the error came from.""" - return f"Invalid '{arg}' value: " - - async def _send_and_wait_request( request: capnp.lib.capnp._Request, # noqa: SLF001 ) -> capnp.lib.capnp._Response: # noqa: SLF001 @@ -259,8 +253,8 @@ async def list_nodes( matching `path` does not fit into given `flags` criteria. Raises: - TypeError: If `path` is not a string. - LabOneCoreError: If invalid `flags` value is given. + TypeError: If `path` is not a string or `flags` is not an integer. + ValueError: If `flags` value is out-of-bounds. LabOneConnectionError: If there is a problem in the connection. Examples: @@ -295,8 +289,12 @@ async def list_nodes( raise TypeError(msg) # noqa: TRY200, B904 try: request.flags = int(flags) - except Exception as error: # noqa: BLE001 - convert_dynamic_schema_error(error, _argument_error_msg_prefix("flags")) + except capnp.KjException as error: # noqa: BLE001 + flags_type = "uint32" + raise ValueError(f"`flags` value is out-of-bounds, it must be of type {flags_type}.") from error + except (TypeError, ValueError) as error: + msg = "`flags` must be an integer." + raise TypeError(msg) from error response = await _send_and_wait_request(request) return list(response.paths) @@ -324,8 +322,8 @@ async def list_nodes_info( matching `path` does not fit into given `flags` criteria. Raises: - TypeError: If `path` is not a string. - LabOneCoreError: If invalid `flags` value is given. + TypeError: If `path` is not a string or `flags` is not an integer. + ValueError: If `flags` value is out-of-bounds. LabOneConnectionError: If there is a problem in the connection. Example: @@ -380,7 +378,11 @@ async def list_nodes_info( raise TypeError(msg) # noqa: TRY200, B904 try: request.flags = int(flags) - except Exception as error: # noqa: BLE001 - convert_dynamic_schema_error(error, _argument_error_msg_prefix("flags")) + except capnp.KjException as error: # noqa: BLE001 + flags_type = "uint32" + raise ValueError(f"`flags` value is out-of-bounds, it must be of type {flags_type}.") from error + except (TypeError, ValueError) as error: + msg = "`flags` must be an integer." + raise TypeError(msg) from error response = await _send_and_wait_request(request) return json.loads(response.nodeProps) diff --git a/tests/core/test_error_handlers.py b/tests/core/test_error_handlers.py deleted file mode 100644 index 6053546..0000000 --- a/tests/core/test_error_handlers.py +++ /dev/null @@ -1,31 +0,0 @@ -"""Tests for `labone.core._error_handlers module.""" -import capnp -import pytest -from labone.core import errors -from labone.core._error_handlers import schema_errors - - -class TestConvertDynamicSchemaError: - def test_no_prefix(self): - with pytest.raises(errors.LabOneCoreError, match="test message"): - schema_errors.convert_dynamic_schema_error(RuntimeError("test message")) - - def test_with_prefix(self): - msg_prefix = "my error: " - orig_msg = "test message" - out_msg = "my error: test message" - with pytest.raises(errors.LabOneCoreError, match=out_msg): - schema_errors.convert_dynamic_schema_error( - RuntimeError(orig_msg), - msg_prefix=msg_prefix, - ) - - def test_with_kj_exception(self): - msg_prefix = "my error: " - orig_msg = "test message" - err = capnp.lib.capnp.KjException(orig_msg) - with pytest.raises(errors.LabOneCoreError, match="my error: test message"): - schema_errors.convert_dynamic_schema_error( - err, - msg_prefix=msg_prefix, - ) diff --git a/tests/core/test_session.py b/tests/core/test_session.py index 424cc7c..1fd01ba 100644 --- a/tests/core/test_session.py +++ b/tests/core/test_session.py @@ -100,17 +100,17 @@ async def test_with_flags_int(self, session_server, flags): assert r == [] @utils.ensure_event_loop - @pytest.mark.parametrize("flags", ["foo", 1.2, [3], None]) + @pytest.mark.parametrize("flags", ["foo", [3], None]) async def test_with_flags_type_error(self, session_server, flags): session, _ = await session_server - with pytest.raises(errors.LabOneCoreError): + with pytest.raises(TypeError): await session.list_nodes("path", flags=flags) @utils.ensure_event_loop @pytest.mark.parametrize("flags", [-2, -100]) async def test_with_flags_value_error(self, session_server, flags): session, _ = await session_server - with pytest.raises(errors.LabOneCoreError): + with pytest.raises(ValueError): await session.list_nodes("path", flags=flags) @@ -163,17 +163,17 @@ async def test_with_flags_int(self, session_server, flags): assert r == {} @utils.ensure_event_loop - @pytest.mark.parametrize("flags", ["foo", 1.2, [3], None]) + @pytest.mark.parametrize("flags", ["foo", [3], None]) async def test_with_flags_type_error(self, session_server, flags): session, _ = await session_server - with pytest.raises(errors.LabOneCoreError): + with pytest.raises(TypeError): await session.list_nodes_info("path", flags=flags) @utils.ensure_event_loop @pytest.mark.parametrize("flags", [-2, -100]) async def test_with_flags_value_error(self, session_server, flags): session, _ = await session_server - with pytest.raises(errors.LabOneCoreError): + with pytest.raises(ValueError): await session.list_nodes_info("path", flags=flags)