异常处理

建议统一使用lightning的BusinessException,已被较好的封装有做了捕捉处理,前端界面的提示也较为完善。

使用方式:

直接使用#

from lightning.exceptions import BusinessException
raise BusinessException('90001', '非法操作')

继承#

from lightning.exceptions import BusinessException
class MyException(BusinessException):
def __init__(self, arg):
super.__init__('90001', '非法操作')
self.arg = arg
raise MyException('Got you')

BusinessException 构造参数:

  • error_code: 错误代码,可自定义,字符串类型。
  • error_message:错误信息。字符串类型。
  • error_data:错误数据,任何可序列化的对象。
  • error_app:错误的 app_label,要求字符串类型。