fix: unique violations
Directly using Psycopg error instead of the error code. Also, raise all other IntegrityErrors instead of silently dropping them.
This commit is contained in:
parent
3433ba39ee
commit
3e4f68dd9b
3 changed files with 12 additions and 5 deletions
|
|
@ -20,6 +20,7 @@ Endpoints:
|
|||
|
||||
from fastapi import APIRouter, status, BackgroundTasks
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from psycopg.errors import UniqueViolation
|
||||
|
||||
from src.iam.exceptions import GroupNotFoundException
|
||||
from src.organisation.exceptions import OrgNotFoundException
|
||||
|
|
@ -283,10 +284,11 @@ async def create_group(
|
|||
db.flush()
|
||||
except IntegrityError as e:
|
||||
if (
|
||||
getattr(e.orig, "pgcode", None) == "23505" # Postgres unique violation
|
||||
isinstance(e.orig, UniqueViolation) # Postgres unique violation
|
||||
or "UNIQUE constraint failed" in str(e.orig) # SQLite unique violation
|
||||
):
|
||||
raise ConflictException("Group with this name already exists")
|
||||
raise
|
||||
group_response = GroupSummary(**group_model.__dict__)
|
||||
org_response = OrgSummary(**org_model.__dict__)
|
||||
db.commit()
|
||||
|
|
@ -500,10 +502,11 @@ async def create_new_permission(
|
|||
db.flush()
|
||||
except IntegrityError as e:
|
||||
if (
|
||||
getattr(e.orig, "pgcode", None) == "23505" # Postgres unique violation
|
||||
isinstance(e.orig, UniqueViolation) # Postgres unique violation
|
||||
or "UNIQUE constraint failed" in str(e.orig) # SQLite unique violation
|
||||
):
|
||||
raise ConflictException(message="Permission already exists")
|
||||
raise
|
||||
response = {
|
||||
"id": perm_model.id,
|
||||
"service_name": perm_model.service_name,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue