Files
wecom_it_smart_desk/backend/app/services/automation/exception_handler.py
T

41 lines
1.6 KiB
Python
Raw Normal View History

# =============================================================================
# 企微IT智能服务台 — 阶段5 自动化 异常定义
# =============================================================================
# 说明:定义自动化引擎内部异常 AutomationException(携带数值错误码),
# 以及 to_app_exception 转换为项目统一的 AppException
# 使全局异常处理器能输出 {code, data, message} 标准格式。
# =============================================================================
from __future__ import annotations
from typing import Any, Optional
from app.constants import AutomationErrorCode, automation_error_message
from app.utils.response import AppException
class AutomationException(Exception):
"""自动化引擎内部异常。
Attributes:
code: 自动化错误码(AutomationErrorCode 取值)
message: 错误消息(缺省取错误码默认文案)
data: 附加数据
"""
def __init__(self, code: int, message: str = "", data: Any = None):
self.code = code
self.message = message or automation_error_message(code)
self.data = data
super().__init__(self.message)
def to_app_exception(exc: AutomationException) -> AppException:
"""将 AutomationException 转为项目统一的 AppException。"""
return AppException(code=exc.code, message=exc.message, data=exc.data)
def automation_error_code(code: int) -> int:
"""校验并返回合法的错误码(占位,便于未来扩展白名单)。"""
return code