mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-10-15 05:19:17 +00:00
refactor(backend): 错误码收敛为单一事实来源,统一异常写法
错误码(t16)
- HTTP 语义只由 core/exceptions.py 的 RET.code → fastapi.status 映射决定;
映射表显式登记 -1→400 与全部「数值即 HTTP 状态」的 RET 成员,_DEFAULT_BUSINESS_STATUS
降为「新增码忘登记」的安全网
- 移除冗余 status_code:131 处调用点改为纯 code 表达;security.py/dependencies.py
的 status_code=401/403 去除(映射表已给);get_or_404 → code=RET.NOT_FOUND.code
- 新增静态护栏(AST):app/ 下除 exceptions.py 外不得出现 status_code 传参,
路由装饰器 201 白名单显式放行并断言白名单非空
异常写法统一(t19)
- 删除 CustomException.internal 及 132 处包装:意外异常直接冒泡,由全局
@app.exception_handler(Exception) 统一映射 5xx + 通用文案 + 日志;
需要运维上下文处改为 logger.exception("<业务动作>失败") + raise
- 构造器不再接受 status_code 参数(属性保留,由 code 派生)→ 结构上杜绝第二来源
- 修复 6 处「宽泛 except 把 400 包成 500」的语义反转(补 except CustomException: raise),
并加 AST 护栏防止回潮
- 全局 SQLAlchemyError:非完整性错误由 400 改为 503(连接类)/500(其它),
不再把驱动原文/异常类型拼进客户端 msg;顺带修掉不存在的
status.HTTP_403_SERVICE_UNAVAILABLE 常量(命中即 AttributeError→500)
验证:pytest 97 passed / ruff 全绿;AST 审计残留 0;护栏红绿测试通过;
线上实测验证码过期仍 400 并保留业务文案、无令牌 401。
This commit is contained in:
@@ -17,7 +17,7 @@ from cryptography.hazmat.primitives import hashes
|
||||
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
|
||||
|
||||
from app.config.setting import settings
|
||||
from app.core.exceptions import CustomException
|
||||
from app.core.logger import logger
|
||||
|
||||
# HKDF 域分隔:与 SECRET_KEY 用于其它目的时派生出不同密钥
|
||||
_INFO = b"fastapiadmin:data-encryption:v1"
|
||||
@@ -71,7 +71,8 @@ class CryptoUtil:
|
||||
return fernet.decrypt(cipher.encode("utf-8")).decode("utf-8")
|
||||
except (InvalidToken, ValueError):
|
||||
continue
|
||||
raise CustomException(msg="敏感数据解密失败,可能原因:DATA_ENCRYPTION_KEY/SECRET_KEY 变更或数据损坏", status_code=500)
|
||||
logger.error("敏感数据解密失败,可能原因:DATA_ENCRYPTION_KEY/SECRET_KEY 变更或数据损坏")
|
||||
raise RuntimeError("敏感数据解密失败,可能原因:DATA_ENCRYPTION_KEY/SECRET_KEY 变更或数据损坏")
|
||||
|
||||
@classmethod
|
||||
def decrypt_or_keep(cls, value: str | None) -> str:
|
||||
|
||||
@@ -8,7 +8,7 @@ from typing import Any
|
||||
from sqlalchemy import inspect as sa_inspect
|
||||
|
||||
from app.config.path_conf import BASE_DIR
|
||||
from app.core.exceptions import CustomException
|
||||
from app.core.logger import logger
|
||||
|
||||
|
||||
class ImportUtil:
|
||||
@@ -153,8 +153,9 @@ class ImportUtil:
|
||||
except ImportError as e:
|
||||
if "cannot import name" not in str(e):
|
||||
raise ImportError(f"❗️ 警告: 无法导入模块 {module_name}: {e}")
|
||||
except Exception as e:
|
||||
raise CustomException(f"❌️ 处理模块 {module_name} 时出错: {e}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('处理模块失败')
|
||||
raise
|
||||
|
||||
# 查找apscheduler_jobs表的模型(如果存在)
|
||||
cls._find_apscheduler_model(base_class, models, seen_models, seen_tables)
|
||||
@@ -204,5 +205,6 @@ class ImportUtil:
|
||||
print(f"✅️ 找到有效模型: {obj.__module__}.{obj.__name__} (表: apscheduler_jobs)")
|
||||
except ImportError:
|
||||
pass
|
||||
except Exception as e:
|
||||
raise CustomException(f"❗️ 查找APScheduler模型时出错: {e}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('查找 APScheduler 模型失败')
|
||||
raise
|
||||
|
||||
@@ -468,7 +468,7 @@ class UploadUtil:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"文件上传失败: {e}")
|
||||
raise CustomException(msg=f"文件上传失败: {e}", status_code=500)
|
||||
raise
|
||||
|
||||
@staticmethod
|
||||
def get_file_tree(file_path: str) -> list[dict]:
|
||||
|
||||
Reference in New Issue
Block a user