Files
live-hub-py/web/backend/errors.py
T
yml2213 2f5a63ae27 feat(auth): 新增手机号与 DCAuth 设备认证登录
- 登录链路改为用户名 + 密码 + 短信验证码 + DCAuth v2 设备签名
- 新增 login_sms_codes / user_devices / device_challenges 三张表,迁移 0063/0064
- 设备绑定只比对 deviceId + publicKey,每账号一个 active 设备,管理员可重置
- 保留未绑定手机号账号的密码登录兜底,绑定后强制安全登录
- 登录页与用户管理页支持验证码、DCAuth 轮询、手机号绑定与设备重置
2026-10-21 22:00:03 +08:00

27 lines
872 B
Python

"""统一的 API 业务异常。
前端需要区分「手机号未绑定」「设备不一致」「验证码锁定」等场景,
FastAPI 默认的 ``{"detail": "..."}`` 无法携带机器可读的错误码,这里补一个
``errorCode`` 字段;HTTP 状态码仍是标准语义(401/403/409/426/429)。
"""
from __future__ import annotations
from fastapi import Request
from fastapi.responses import JSONResponse
class ApiError(Exception):
def __init__(self, status_code: int, detail: str, error_code: str):
super().__init__(detail)
self.status_code = status_code
self.detail = detail
self.error_code = error_code
async def api_error_handler(_: Request, exc: ApiError) -> JSONResponse:
return JSONResponse(
status_code=exc.status_code,
content={"detail": exc.detail, "errorCode": exc.error_code},
)