Skip to content

Commit 5604886

Browse files
committed
@pcrespov review: use more robust checks
1 parent b7d1d2c commit 5604886

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,23 @@
3030

3131
logger = logging.getLogger(__name__)
3232

33-
_POSTGRES_ERROR_TO_ERROR_MAP: Final[
34-
dict[tuple[str, ...], tuple[type[DirectorError], tuple[str, ...]]]
33+
_POSTGRES_FK_COLUMN_TO_ERROR_MAP: Final[
34+
dict[sa.Column, tuple[type[DirectorError], tuple[str, ...]]]
3535
] = {
36-
("users", "user_id"): (UserNotFoundError, ("users", "user_id")),
37-
("projects", "project_uuid"): (
36+
comp_runs.c.user_id: (UserNotFoundError, ("users", "user_id")),
37+
comp_runs.c.project_uuid: (
3838
ProjectNotFoundError,
3939
("projects", "project_id"),
4040
),
41-
("clusters", "cluster_id"): (
41+
comp_runs.c.cluster_id: (
4242
ClusterNotFoundError,
4343
("clusters", "cluster_id"),
4444
),
4545
}
46+
_DEFAULT_FK_CONSTRAINT_TO_ERROR: Final[tuple[type[DirectorError], tuple]] = (
47+
DirectorError,
48+
(),
49+
)
4650

4751

4852
class CompRunsRepository(BaseRepository):
@@ -186,10 +190,13 @@ async def create(
186190
row = await result.first()
187191
return CompRunsAtDB.model_validate(row)
188192
except ForeignKeyViolation as exc:
189-
message = exc.args[0]
190-
191-
for pg_keys, (exc_type, exc_keys) in _POSTGRES_ERROR_TO_ERROR_MAP.items():
192-
if all(k in message for k in pg_keys):
193+
assert exc.diag.constraint_name # nosec # noqa: PT017
194+
for foreign_key in comp_runs.foreign_keys:
195+
if exc.diag.constraint_name == foreign_key.name:
196+
assert foreign_key.parent is not None # nosec
197+
exc_type, exc_keys = _POSTGRES_FK_COLUMN_TO_ERROR_MAP[
198+
foreign_key.parent
199+
]
193200
raise exc_type(
194201
**{f"{k}": locals().get(k) for k in exc_keys}
195202
) from exc

services/director-v2/tests/unit/with_dbs/test_api_route_computations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ async def test_get_computation_from_not_started_computation_task(
927927
f"/v2/computations/{proj.uuid}?user_id={user['id']}"
928928
)
929929
await create_pipeline(
930-
project_id=proj.uuid,
930+
project_id=f"{proj.uuid}",
931931
dag_adjacency_list=fake_workbench_adjacency,
932932
)
933933
# create no task this should trigger an exception

0 commit comments

Comments
 (0)