Files
trade-assistant/backend/app/core/exceptions.py
T
TradeMate Dev c6206787da Initial commit: TradeMate 外贸小助手 MVP
项目结构:
- backend/     Python FastAPI 后端
- uni-app/     uni-app跨端前端
- docs/        设计文档
- docker-compose.yml  Docker编排
- nginx/scripts/systemd 运维配置

已完成功能:
- 用户认证 (JWT)
- 智能翻译 + 回复建议
- 营销素材生成
- 客户管理 + 沉默检测
- 报价单管理
- 产品库管理
- 汇率换算
- 推送通知 (uni-push)
- WhatsApp Webhook框架
- Celery定时任务
2026-05-08 18:17:12 +08:00

59 lines
1.9 KiB
Python

from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
class TradeMateException(Exception):
def __init__(self, code: int, message: str, detail: str = None):
self.code = code
self.message = message
self.detail = detail
class NotFoundError(TradeMateException):
def __init__(self, resource: str = "Resource"):
super().__init__(404, f"{resource} not found")
class UnauthorizedError(TradeMateException):
def __init__(self, detail: str = "Authentication required"):
super().__init__(401, "Unauthorized", detail)
class ForbiddenError(TradeMateException):
def __init__(self, detail: str = "Insufficient permissions"):
super().__init__(403, "Forbidden", detail)
class QuotaExceededError(TradeMateException):
def __init__(self, feature: str):
super().__init__(429, "Quota exceeded", f"Daily limit reached for {feature}. Upgrade to Pro for more.")
class TierRestrictionError(TradeMateException):
def __init__(self, feature: str, required_tier: str):
super().__init__(
402,
"Upgrade required",
f"{feature} requires {required_tier} plan",
)
def register_exception_handlers(app: FastAPI):
@app.exception_handler(TradeMateException)
async def handle_tradmate_exception(request: Request, exc: TradeMateException):
return JSONResponse(
status_code=exc.code,
content={
"error": exc.message,
"detail": exc.detail,
"code": exc.code,
},
)
@app.exception_handler(Exception)
async def handle_generic_exception(request: Request, exc: Exception):
return JSONResponse(
status_code=500,
content={"error": "Internal server error", "detail": str(exc) if app.debug else "An unexpected error occurred"},
)