feat: add accessory API routes and exception handlers
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
118
app/main.py
118
app/main.py
@@ -18,6 +18,7 @@ except Exception as e:
|
||||
|
||||
from app.api.v1.location import router as location_router
|
||||
from app.api.v1.box import router as box_router
|
||||
from app.api.v1.accessory import router as accessory_router
|
||||
from app.services.location_service import (
|
||||
AlreadyOffShelfError,
|
||||
DuplicateLocationError,
|
||||
@@ -31,11 +32,22 @@ from app.services.box_service import (
|
||||
InvalidZongpaiError,
|
||||
ZongpaiNotFoundError,
|
||||
)
|
||||
from app.services.accessory_service import (
|
||||
AccessoryAlreadyBoxedError,
|
||||
AccessoryAlreadyOffShelfError,
|
||||
AccessoryDuplicateLocationError,
|
||||
AccessoryNotFoundError,
|
||||
AccessoryTypeNotFoundError,
|
||||
DuplicateAccessoryTypeNameError,
|
||||
InvalidPaichanError,
|
||||
PaichanNotFoundError as AccessoryPaichanNotFoundError,
|
||||
)
|
||||
|
||||
app = FastAPI(title="CargoTrace API", version="0.1.0")
|
||||
|
||||
app.include_router(location_router, prefix="/CargoTrace")
|
||||
app.include_router(box_router, prefix="/CargoTrace")
|
||||
app.include_router(accessory_router, prefix="/CargoTrace")
|
||||
|
||||
|
||||
@app.exception_handler(DuplicateLocationError)
|
||||
@@ -68,7 +80,7 @@ async def already_off_shelf_handler(request: Request, exc: AlreadyOffShelfError)
|
||||
async def validation_error_handler(request: Request, exc: RequestValidationError):
|
||||
for err in exc.errors():
|
||||
msg = err.get("msg", "")
|
||||
if "INVALID_ZONGPAI" in msg or "INVALID_LOCATION" in msg or "INVALID_BOX_NO" in msg or "INVALID_QUANTITY" in msg:
|
||||
if "INVALID_ZONGPAI" in msg or "INVALID_LOCATION" in msg or "INVALID_BOX_NO" in msg or "INVALID_QUANTITY" in msg or "INVALID_PAICHAN" in msg or "INVALID_TYPE_NAME" in msg:
|
||||
error_code = msg.replace("Value error, ", "")
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
@@ -174,6 +186,110 @@ async def invalid_box_item_handler(request: Request, exc: InvalidBoxItemError):
|
||||
)
|
||||
|
||||
|
||||
@app.exception_handler(InvalidPaichanError)
|
||||
async def invalid_paichan_handler(request: Request, exc: InvalidPaichanError):
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={
|
||||
"error_code": "INVALID_PAICHAN",
|
||||
"message": "排产号格式不合法",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@app.exception_handler(AccessoryPaichanNotFoundError)
|
||||
async def accessory_paichan_not_found_handler(
|
||||
request: Request, exc: AccessoryPaichanNotFoundError
|
||||
):
|
||||
return JSONResponse(
|
||||
status_code=404,
|
||||
content={
|
||||
"error_code": "PAICHA_NOT_FOUND",
|
||||
"message": "未找到该排产号信息",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@app.exception_handler(AccessoryNotFoundError)
|
||||
async def accessory_not_found_handler(request: Request, exc: AccessoryNotFoundError):
|
||||
return JSONResponse(
|
||||
status_code=404,
|
||||
content={
|
||||
"error_code": "ACCESSORY_NOT_FOUND",
|
||||
"message": "附件记录不存在",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@app.exception_handler(AccessoryAlreadyBoxedError)
|
||||
async def accessory_already_boxed_handler(
|
||||
request: Request, exc: AccessoryAlreadyBoxedError
|
||||
):
|
||||
return JSONResponse(
|
||||
status_code=409,
|
||||
content={
|
||||
"error_code": "ALREADY_BOXED",
|
||||
"message": "该附件已装箱不可操作",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@app.exception_handler(AccessoryDuplicateLocationError)
|
||||
async def accessory_duplicate_location_handler(
|
||||
request: Request, exc: AccessoryDuplicateLocationError
|
||||
):
|
||||
return JSONResponse(
|
||||
status_code=409,
|
||||
content={
|
||||
"error_code": "DUPLICATE_LOCATION",
|
||||
"message": "该附件已存在货位记录",
|
||||
"location_code": exc.location_code,
|
||||
"registered_at": exc.registered_at,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@app.exception_handler(AccessoryAlreadyOffShelfError)
|
||||
async def accessory_already_off_shelf_handler(
|
||||
request: Request, exc: AccessoryAlreadyOffShelfError
|
||||
):
|
||||
return JSONResponse(
|
||||
status_code=409,
|
||||
content={
|
||||
"error_code": "ALREADY_OFF_SHELF",
|
||||
"message": "该附件已下架不可重新上架",
|
||||
"location_code": exc.location_code,
|
||||
"registered_at": exc.registered_at,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@app.exception_handler(AccessoryTypeNotFoundError)
|
||||
async def accessory_type_not_found_handler(
|
||||
request: Request, exc: AccessoryTypeNotFoundError
|
||||
):
|
||||
return JSONResponse(
|
||||
status_code=404,
|
||||
content={
|
||||
"error_code": "ACCESSORY_TYPE_NOT_FOUND",
|
||||
"message": "附件类型不存在",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@app.exception_handler(DuplicateAccessoryTypeNameError)
|
||||
async def duplicate_accessory_type_name_handler(
|
||||
request: Request, exc: DuplicateAccessoryTypeNameError
|
||||
):
|
||||
return JSONResponse(
|
||||
status_code=409,
|
||||
content={
|
||||
"error_code": "DUPLICATE_TYPE_NAME",
|
||||
"message": "附件类型名称已存在",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def root():
|
||||
return {"message": "Welcome to CargoTrace API"}
|
||||
|
||||
Reference in New Issue
Block a user