fix: Day2就绪 — 删除硬化+索引补全+全局异常处理
## base_service.py — MaterialBase删除硬化 - 新增 BomTable 引用检查(bom_usage_count) - 物料被BOM引用时禁止删除,提示「请先解除BOM绑定」 ## models/base.py + bom.py — 缺失索引补全 - material_base: company_name + is_enabled 添加 index=True - bom_table: is_enabled 添加 index=True ## __init__.py — 全局异常处理 - @app.errorhandler(ValueError) → 400 + 自定义消息 - @app.errorhandler(404/405) → 标准错误 - @app.errorhandler(Exception) → 500 + 通用消息 堆栈仅记录服务器日志,不泄露到前端
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
# 文件路径: inventory-backend/app/__init__.py
|
||||
|
||||
from flask import Flask
|
||||
from flask import Flask, jsonify
|
||||
from config import Config
|
||||
from app.extensions import db, migrate, cors, jwt
|
||||
from app.api.v1.scan import scan_bp
|
||||
@ -295,4 +295,33 @@ def create_app():
|
||||
except Exception as e:
|
||||
print(f"⚠️ 模型预加载发生未知错误: {e}")
|
||||
|
||||
# =========================================================
|
||||
# 4. 全局异常处理(面向生产:不泄露堆栈)
|
||||
# =========================================================
|
||||
import logging
|
||||
import traceback
|
||||
|
||||
@app.errorhandler(ValueError)
|
||||
def handle_value_error(e):
|
||||
"""业务校验异常 → 400 + 自定义消息"""
|
||||
return jsonify({'code': 400, 'msg': str(e)}), 400
|
||||
|
||||
@app.errorhandler(404)
|
||||
def handle_not_found(e):
|
||||
return jsonify({'code': 404, 'msg': '资源不存在'}), 404
|
||||
|
||||
@app.errorhandler(405)
|
||||
def handle_method_not_allowed(e):
|
||||
return jsonify({'code': 405, 'msg': '请求方法不允许'}), 405
|
||||
|
||||
@app.errorhandler(Exception)
|
||||
def handle_unhandled_exception(e):
|
||||
"""兜底:任何未捕获异常 → 500 + 通用消息,堆栈仅记录日志"""
|
||||
app.logger.error(f"Unhandled exception: {type(e).__name__}: {e}\n{traceback.format_exc()}")
|
||||
return jsonify({
|
||||
'code': 500,
|
||||
'msg': '系统繁忙,请稍后再试',
|
||||
'error': 'Internal Server Error'
|
||||
}), 500
|
||||
|
||||
return app
|
||||
|
||||
Reference in New Issue
Block a user