refactor: use sqlite3.IntegrityError directly for slug collision guard
This commit is contained in:
parent
69e1b70072
commit
86dd9adbcb
1 changed files with 6 additions and 8 deletions
|
|
@ -6,6 +6,7 @@ from __future__ import annotations
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
|
import sqlite3
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, HTTPException, Request, Response
|
from fastapi import APIRouter, Depends, HTTPException, Request, Response
|
||||||
|
|
@ -222,14 +223,11 @@ async def publish_post(body: dict, session: CloudUser = Depends(get_session)):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
inserted = await asyncio.to_thread(store.insert_post, post)
|
inserted = await asyncio.to_thread(store.insert_post, post)
|
||||||
except Exception as exc:
|
except sqlite3.IntegrityError as exc:
|
||||||
exc_str = str(exc).lower()
|
|
||||||
if "unique" in exc_str or "duplicate" in exc_str:
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=409,
|
status_code=409,
|
||||||
detail="A post with this title already exists today. Try a different title.",
|
detail="A post with this title already exists today. Try a different title.",
|
||||||
) from exc
|
) from exc
|
||||||
raise
|
|
||||||
return _post_to_dict(inserted)
|
return _post_to_dict(inserted)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue