Skip to content

Commit 3c93db2

Browse files
committed
@pcrespov review: remove match and use a mapping
1 parent 7f279c5 commit 3c93db2

File tree

1 file changed

+22
-11
lines changed
  • services/director-v2/src/simcore_service_director_v2/modules/db/repositories

1 file changed

+22
-11
lines changed

services/director-v2/src/simcore_service_director_v2/modules/db/repositories/comp_runs.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import datetime
22
import logging
3-
from typing import Any
3+
from typing import Any, Final
44

55
import arrow
66
import sqlalchemy as sa
@@ -20,7 +20,6 @@
2020
ClusterNotFoundError,
2121
ComputationalRunNotFoundError,
2222
DirectorError,
23-
ProjectNotFoundError,
2423
UserNotFoundError,
2524
)
2625
from ....models.comp_runs import CompRunsAtDB, RunMetadataDict
@@ -30,6 +29,20 @@
3029

3130
logger = logging.getLogger(__name__)
3231

32+
_POSTGRES_ERROR_TO_ERROR_MAP: Final[
33+
dict[tuple[str, ...], tuple[type[DirectorError], tuple[str, ...]]]
34+
] = {
35+
("users", "user_id"): (UserNotFoundError, ("users", "user_id")),
36+
("projects", "project_uuid"): (
37+
UserNotFoundError,
38+
("projects", "project_id"),
39+
),
40+
("clusters", "cluster_id"): (
41+
ClusterNotFoundError,
42+
("clusters", "cluster_id"),
43+
),
44+
}
45+
3346

3447
class CompRunsRepository(BaseRepository):
3548
async def get(
@@ -173,15 +186,13 @@ async def create(
173186
return CompRunsAtDB.model_validate(row)
174187
except ForeignKeyViolation as exc:
175188
message = exc.args[0]
176-
match message:
177-
case s if "users" in s and "user_id" in s:
178-
raise UserNotFoundError(user_id=user_id) from exc
179-
case s if "projects" in s and "project_uuid" in s:
180-
raise ProjectNotFoundError(project_id=project_id) from exc
181-
case s if "clusters" in s and "cluster_id" in s:
182-
raise ClusterNotFoundError(cluster_id=cluster_id) from exc
183-
case _:
184-
raise DirectorError from exc
189+
190+
for pg_keys, (exc_type, exc_keys) in _POSTGRES_ERROR_TO_ERROR_MAP.items():
191+
if all(k in message for k in pg_keys):
192+
raise exc_type(
193+
**{f"{k}": locals().get(k) for k in exc_keys}
194+
) from exc
195+
raise DirectorError from exc
185196

186197
async def update(
187198
self, user_id: UserID, project_id: ProjectID, iteration: PositiveInt, **values

0 commit comments

Comments
 (0)