fix: unify error response format with error_code, message and domain fields

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Misaka_Company
2026-05-11 12:38:37 +08:00
parent 6a4ea34e24
commit e9bb9d6be0
5 changed files with 72 additions and 27 deletions

View File

@@ -1,10 +1,17 @@
from fastapi import HTTPException
from sqlalchemy.orm import Session
from app.models.finished_goods import FinishedGoodsLocation
from app.schemas.location import LocationRequest
class DuplicateLocationError(Exception):
"""总排号已存在货位记录。"""
def __init__(self, location_code: str, registered_at: str):
self.location_code = location_code
self.registered_at = registered_at
def register_location(db: Session, req: LocationRequest) -> FinishedGoodsLocation:
existing = (
db.query(FinishedGoodsLocation)
@@ -12,16 +19,9 @@ def register_location(db: Session, req: LocationRequest) -> FinishedGoodsLocatio
.first()
)
if existing:
raise HTTPException(
status_code=409,
detail={
"error_code": "DUPLICATE_LOCATION",
"message": "该总排号已存在货位记录",
"detail": {
"existing_location": existing.location_code,
"created_at": existing.created_at.isoformat(),
},
},
raise DuplicateLocationError(
location_code=existing.location_code,
registered_at=existing.created_at.isoformat(),
)
record = FinishedGoodsLocation(