Skip to content

Commit b41b705

Browse files
author
Andrei Neagu
committed
refactored errors
1 parent 36fa251 commit b41b705

File tree

5 files changed

+34
-41
lines changed

5 files changed

+34
-41
lines changed

services/dynamic-sidecar/src/simcore_service_dynamic_sidecar/core/docker_utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ async def get_volume_by_label(label: str, run_id: RunID) -> dict[str, Any]:
5050
volumes = data["Volumes"]
5151
_logger.debug("volumes query for label=%s volumes=%s", label, volumes)
5252
if len(volumes) != 1:
53-
raise VolumeNotFoundError(label, run_id, volumes)
53+
raise VolumeNotFoundError(
54+
volume_count=len(volumes),
55+
source_label=label,
56+
run_id=run_id,
57+
volume_names=" ".join(v.get("Name", "UNKNOWN") for v in volumes),
58+
status_code=http_status.HTTP_404_NOT_FOUND,
59+
)
5460
volume_details: dict[str, Any] = volumes[0]
5561
return volume_details
5662

services/dynamic-sidecar/src/simcore_service_dynamic_sidecar/core/error_handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ async def http_error_handler(
1212
) -> JSONResponse:
1313
return JSONResponse(
1414
content=jsonable_encoder({"errors": [exception.message]}),
15-
status_code=exception.status_code,
15+
status_code=exception.status_code, # type:ignore[attr-defined]
1616
)
1717

1818

services/dynamic-sidecar/src/simcore_service_dynamic_sidecar/core/errors.py

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,30 @@
1-
from typing import Any
2-
31
from common_library.errors_classes import OsparcErrorMixin
4-
from fastapi import status
5-
from models_library.services import RunID
62

73

8-
class BaseDynamicSidecarError(Exception):
4+
class BaseDynamicSidecarError(OsparcErrorMixin, Exception):
95
"""Used as base for all exceptions"""
106

11-
def __init__(
12-
self, nessage: str, status_code: int = status.HTTP_500_INTERNAL_SERVER_ERROR
13-
) -> None:
14-
self.message: str = nessage
15-
self.status_code: int = status_code
16-
super().__init__(nessage)
17-
187

198
class VolumeNotFoundError(BaseDynamicSidecarError):
20-
def __init__(
21-
self, source_label: str, run_id: RunID, volumes: list[dict[str, Any]]
22-
) -> None:
23-
super().__init__(
24-
f"Expected 1 got {len(volumes)} volumes labels with {source_label=}, {run_id=}: "
25-
f"Found {' '.join(v.get('Name', 'UNKNOWN') for v in volumes)}",
26-
status_code=status.HTTP_404_NOT_FOUND,
27-
)
9+
msg_template = (
10+
"Expected 1 got {volume_count} volumes labels with "
11+
"source_label={source_label}, run_id={run_id}: Found {volume_names}"
12+
)
2813

2914

3015
class UnexpectedDockerError(BaseDynamicSidecarError):
31-
def __init__(self, message: str, status_code: int) -> None:
32-
super().__init__(
33-
f"An unexpected Docker error occurred {status_code=}, {message=}",
34-
status_code=status_code,
35-
)
36-
37-
38-
class BaseError(OsparcErrorMixin, BaseDynamicSidecarError):
39-
...
16+
msg_template = "An unexpected Docker error occurred status_code={status_code}, message={message}"
4017

4118

42-
class ContainerExecContainerNotFoundError(BaseError):
19+
class ContainerExecContainerNotFoundError(BaseDynamicSidecarError):
4320
msg_template = "Container '{container_name}' was not found"
4421

4522

46-
class ContainerExecTimeoutError(BaseError):
23+
class ContainerExecTimeoutError(BaseDynamicSidecarError):
4724
msg_template = "Timed out after {timeout} while executing: '{command}'"
4825

4926

50-
class ContainerExecCommandFailedError(BaseError):
27+
class ContainerExecCommandFailedError(BaseDynamicSidecarError):
5128
msg_template = (
5229
"Command '{command}' exited with code '{exit_code}'"
5330
"and output: '{command_result}'"

services/dynamic-sidecar/src/simcore_service_dynamic_sidecar/core/settings.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from datetime import timedelta
33
from functools import lru_cache
44
from pathlib import Path
5-
from typing import cast
5+
from typing import Annotated, cast
66

77
from common_library.pydantic_validators import validate_numeric_string_as_timedelta
88
from models_library.basic_types import BootModeEnum, PortInt
@@ -53,10 +53,13 @@ class SystemMonitorSettings(BaseCustomSettings):
5353

5454

5555
class ApplicationSettings(BaseCustomSettings, MixinLoggingSettings):
56-
SC_BOOT_MODE: BootModeEnum = Field(
57-
...,
58-
description="boot mode helps determine if in development mode or normal operation",
59-
)
56+
SC_BOOT_MODE: Annotated[
57+
BootModeEnum,
58+
Field(
59+
...,
60+
description="boot mode helps determine if in development mode or normal operation",
61+
),
62+
]
6063

6164
DYNAMIC_SIDECAR_DY_VOLUMES_MOUNT_DIR: Path = Field(
6265
...,

services/dynamic-sidecar/tests/unit/test_core_errors.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,20 @@ def test_legacy_interface_unexpected_docker_error():
2323

2424
def test_legacy_interface_volume_not_found_error():
2525
try:
26+
volumes = [{}, {"Name": "a_volume"}]
27+
volume_names = " ".join(v.get("Name", "UNKNOWN") for v in volumes)
28+
2629
raise VolumeNotFoundError( # noqa: TRY301
27-
source_label="some", run_id="run_id", volumes=[{}, {"Name": "a_volume"}]
30+
volume_count=len(volumes),
31+
source_label="some",
32+
run_id="run_id",
33+
volume_names=volume_names,
34+
status_code=status.HTTP_404_NOT_FOUND,
2835
)
2936
except Exception as e:
3037
print(e)
3138
assert ( # noqa: PT017
3239
e.message
33-
== "Expected 1 got 2 volumes labels with source_label='some', run_id='run_id': Found UNKNOWN a_volume"
40+
== "Expected 1 got 2 volumes labels with source_label=some, run_id=run_id: Found UNKNOWN a_volume"
3441
)
3542
assert e.status_code == status.HTTP_404_NOT_FOUND # noqa: PT017

0 commit comments

Comments
 (0)