- 后端:LX94 量价背离扫描(85 品种×4 周期)、JPush 推送分发、 账号/激活码体系、阿里云短信、管理台、28 项验收脚本 - 客户端:36 页 UI(漆器朱金主题)、自绘三窗格龙虾 K 线、 离线缓存、WorkManager 兜底轮询
57 lines
1.6 KiB
Python
57 lines
1.6 KiB
Python
"""统一响应体与错误码。"""
|
||
from __future__ import annotations
|
||
|
||
from fastapi import HTTPException, Request
|
||
from fastapi.responses import JSONResponse
|
||
|
||
# 业务错误码(HTTP 200)
|
||
PHONE_OR_PASSWORD_WRONG = 1001
|
||
ACCOUNT_LOCKED = 1002
|
||
PHONE_REGISTERED = 1003
|
||
SMS_CODE_WRONG = 1004
|
||
CAPTCHA_WRONG = 1005
|
||
SMS_TOO_FREQUENT = 1006
|
||
PASSWORD_MISMATCH = 1007
|
||
OLD_PASSWORD_WRONG = 1008
|
||
TOKEN_INVALID = 1009 # HTTP 401
|
||
CODE_NOT_FOUND = 2001
|
||
CODE_USED = 2002
|
||
CODE_EXPIRED = 2003
|
||
NOT_ACTIVATED = 2004 # HTTP 403
|
||
BANNED = 2005 # HTTP 403
|
||
CODE_FORMAT_WRONG = 2006
|
||
MARKET_UNAVAILABLE = 3001
|
||
SYMBOL_NO_DATA = 3002
|
||
SYMBOL_INSUFFICIENT = 3003
|
||
TOO_FREQUENT = 9001 # HTTP 429
|
||
INTERNAL = 5000
|
||
|
||
_HTTP_STATUS = {TOKEN_INVALID: 401, NOT_ACTIVATED: 403, BANNED: 403, TOO_FREQUENT: 429}
|
||
|
||
|
||
class BizError(Exception):
|
||
def __init__(self, code: int, message: str, http_status: int | None = None):
|
||
self.code = code
|
||
self.message = message
|
||
self.http_status = http_status or _HTTP_STATUS.get(code, 200)
|
||
super().__init__(message)
|
||
|
||
|
||
def ok(data=None, message: str = "") -> dict:
|
||
return {"code": 0, "data": data, "message": message}
|
||
|
||
|
||
def err(code: int, message: str) -> dict:
|
||
return {"code": code, "data": None, "message": message}
|
||
|
||
|
||
async def biz_error_handler(_: Request, exc: BizError) -> JSONResponse:
|
||
return JSONResponse(status_code=exc.http_status, content=err(exc.code, exc.message))
|
||
|
||
|
||
async def fallback_error_handler(_: Request, exc: Exception) -> JSONResponse:
|
||
import logging
|
||
|
||
logging.getLogger("xl").exception("未处理异常: %s", exc)
|
||
return JSONResponse(status_code=500, content=err(INTERNAL, "服务器内部错误"))
|