forked from sr2/cloud-api
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
|
|
@ -10,6 +10,7 @@ Endpoints:
|
|||
|
||||
from fastapi import APIRouter, status
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from psycopg.errors import UniqueViolation
|
||||
|
||||
from src.exceptions import ConflictException
|
||||
from src.database import db_dependency
|
||||
|
|
@ -110,10 +111,11 @@ async def register_service(
|
|||
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="Service with this name already exists")
|
||||
raise
|
||||
response = ServiceWithKeySchema(**service_model.__dict__)
|
||||
db.commit()
|
||||
return {"service": response}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue