装饰器(跨领域调用时增强异常信息)

  1. 装饰器
python 复制代码
from functools import wraps
import traceback
from odoo import _
from odoo.exceptions import ValidationError
from odoo.http import request

LAST_ITEM_INDEX = -1  # 列表中最后一个元素
# 领域翻译
IR_DOMAIN_TRANSLATE = {
    'fin': _('fin'),  # 财务会计
    'mfg': _('mfg'),  # 生产制造
    'ext': _('ext'),  # 外部系统管理
}


def wrap_module_exceptions(func):
    """
    装饰器:自动为来自其他领域的异常添加路径信息
    
    当异常发生在指定领域(如ext)时,自动在异常消息前添加领域名称前缀。
    主要用于跨领域调用时的异常信息增强。
    """
    @wraps(func)
    def wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except Exception as e:
            # 提取堆栈信息 traceback.FrameSummary列表
            tb_list = traceback.extract_tb(e.__traceback__)
            if not tb_list:
                raise
            # 获取异常最后一帧
            last_frame = tb_list[LAST_ITEM_INDEX]
            
            # 构建新消息
            new_msg = e.args[0] if e.args else str(e)
            # 指定领域
            origin_names = ['ext', 'tax', 'scm', 'cost']
            for origin_name in origin_names:
                name = IR_DOMAIN_TRANSLATE.get(origin_name, '')
                if name and origin_name in last_frame.filename:
                    new_msg = f"[{name}] {new_msg}"
                    break
            
            if isinstance(e, ValidationError):
                # 保留原始异常链
                raise ValidationError(new_msg) from e
            else:
                # 修改异常消息,保持异常对象
                if e.args:
                    e.args = (new_msg,) + e.args[1:]
                else:
                    e = new_msg
            
            # 抛出异常
            raise
    return wrapper
  1. 应用
python 复制代码
from odoo.addons.ps_fin_doc_base.models.exception_handler import wrap_module_exceptions

@wrap_module_exceptions
def hook_after_audit_update(self, *args, **kwargs):
    pass
  1. 日志对比
  • 应用前
python 复制代码
odoo.addons.ps_studio.models.handler.service_handler: ('财务应付日期不能早于暂估应付日期', None) 
Traceback (most recent call last):
  File "/opt/projects/insuite/biz/platform/ps_studio/models/handler/service_handler.py", line 538, in _studio_call_services
    service_result = self._deal_after_service(preset_data, single_record, default_opt_msg)
  File "/opt/projects/insuite/biz/platform/ps_studio/models/handler/service_handler.py", line 716, in _deal_after_service
    all_svc_config = preset_data["after_services"].filtered(lambda svc: 'std_pre_svc_unlock' not in svc.svc_method))
  File "/opt/projects/insuite/biz/platform/ps_studio/models/handler/service_handler.py", line 1092, in p_call_services
    svc_res = self._call_odoo_service(model_key, operation, origin_args, timing, single_record, svc_config)
  File "/opt/projects/insuite/biz/platform/ps_studio/models/handler/service_handler.py", line 1141, in _call_odoo_service
    args = args)
    ####
  File "/opt/projects/insuite/biz/fin/ps_ap/ap_payable/development/models/ap_payable.py", line 1918, in hook_after_audit_update
    self._event("on_ap_payable_listen_target_doc_audit").notify(self,source_model_key,record,source_doc)
  File "/opt/projects/insuite/biz/third-party/OCA-connector/component_event/components/event.py", line 191, in notify
    event(*args, **kwargs)
  File "/opt/projects/insuite/biz/third-party/OCA-connector/component_event/components/event.py", line 160, in func_wrapper
    return func(*args, **kwargs)
  File "/opt/projects/insuite/biz/scm/ps_stk/stk_pur_receipt/development/models/stk_pur_receipt.py", line 3964, in on_ap_payable_listen_target_doc_audit
    self.env['ia.in.result'].update_realcost(tar_docs=tar_docs,is_audit=True,is_return=False)
  File "/opt/projects/insuite/biz/cost/ps_ia/ia_pub/models/ia_in_result.py", line 303, in update_realcost
    raise ValidationError(_('The financial payable date cannot be earlier than the estimated payable date'))
    ####
odoo.exceptions.ValidationError: ('财务应付日期不能早于暂估应付日期', None)
  • 应用后
python 复制代码
odoo.addons.ps_studio.models.handler.service_handler: ('[成本模块] 财务应付日期不能早于暂估应付日期', None) 
Traceback (most recent call last):
  File "/opt/projects/insuite/biz/fin/ps_ap/ap_payable/development/models/ap_payable.py", line 64, in wrapper
    return func(*args, **kwargs)
  File "/opt/projects/insuite/biz/fin/ps_ap/ap_payable/development/models/ap_payable.py", line 1918, in hook_after_audit_update
    self._event("on_ap_payable_listen_target_doc_audit").notify(self,source_model_key,record,source_doc)
  File "/opt/projects/insuite/biz/third-party/OCA-connector/component_event/components/event.py", line 191, in notify
    event(*args, **kwargs)
  File "/opt/projects/insuite/biz/third-party/OCA-connector/component_event/components/event.py", line 160, in func_wrapper
    return func(*args, **kwargs)
  File "/opt/projects/insuite/biz/scm/ps_stk/stk_pur_receipt/development/models/stk_pur_receipt.py", line 3964, in on_ap_payable_listen_target_doc_audit
    self.env['ia.in.result'].update_realcost(tar_docs=tar_docs,is_audit=True,is_return=False)
  File "/opt/projects/insuite/biz/cost/ps_ia/ia_pub/models/ia_in_result.py", line 303, in update_realcost
    raise ValidationError(_('The financial payable date cannot be earlier than the estimated payable date'))
odoo.exceptions.ValidationError: ('财务应付日期不能早于暂估应付日期', None)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/projects/insuite/biz/platform/ps_studio/models/handler/service_handler.py", line 538, in _studio_call_services
    service_result = self._deal_after_service(preset_data, single_record, default_opt_msg)
  File "/opt/projects/insuite/biz/platform/ps_studio/models/handler/service_handler.py", line 716, in _deal_after_service
    all_svc_config = preset_data["after_services"].filtered(lambda svc: 'std_pre_svc_unlock' not in svc.svc_method))
  File "/opt/projects/insuite/biz/platform/ps_studio/models/handler/service_handler.py", line 1092, in p_call_services
    svc_res = self._call_odoo_service(model_key, operation, origin_args, timing, single_record, svc_config)
  File "/opt/projects/insuite/biz/platform/ps_studio/models/handler/service_handler.py", line 1141, in _call_odoo_service
    args = args)
  File "/opt/projects/insuite/biz/fin/ps_ap/ap_payable/development/models/ap_payable.py", line 77, in wrapper
    raise ValidationError(new_msg) from e
odoo.exceptions.ValidationError: ('[成本模块] 财务应付日期不能早于暂估应付日期', None)
相关推荐
冷雨夜中漫步7 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
郝学胜-神的一滴7 小时前
深入解析Python字典的继承关系:从abc模块看设计之美
网络·数据结构·python·程序人生
百锦再7 小时前
Reactive编程入门:Project Reactor 深度指南
前端·javascript·python·react.js·django·前端框架·reactjs
喵手9 小时前
Python爬虫实战:旅游数据采集实战 - 携程&去哪儿酒店机票价格监控完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集结果csv导出·旅游数据采集·携程/去哪儿酒店机票价格监控
2501_944934739 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
helloworldandy9 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
肖永威11 小时前
macOS环境安装/卸载python实践笔记
笔记·python·macos
TechWJ11 小时前
PyPTO编程范式深度解读:让NPU开发像写Python一样简单
开发语言·python·cann·pypto
枷锁—sha11 小时前
【SRC】SQL注入WAF 绕过应对策略(二)
网络·数据库·python·sql·安全·网络安全
abluckyboy11 小时前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法