Skip to content

Commit

Permalink
Raise ValueError for invalid flags values
Browse files Browse the repository at this point in the history
  • Loading branch information
markheik committed Aug 28, 2023
1 parent 7c89fdd commit a39e98b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 92 deletions.
10 changes: 0 additions & 10 deletions src/labone/core/_error_handlers/__init__.py

This file was deleted.

31 changes: 0 additions & 31 deletions src/labone/core/_error_handlers/schema_errors.py

This file was deleted.

30 changes: 16 additions & 14 deletions src/labone/core/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
31 changes: 0 additions & 31 deletions tests/core/test_error_handlers.py

This file was deleted.

12 changes: 6 additions & 6 deletions tests/core/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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)


Expand Down

0 comments on commit a39e98b

Please sign in to comment.