丿忆梦灬微殇 2021-01-30 11:05 采纳率: 0%
浏览 27

在使用drf的时候为什么抛出Exception异常的时候会报TypeError?

 下面是测试抛出异常的代码:

                for index, IotId in enumerate(kwargs['IotId_list']):
                    if IotId:
                        raise Exception('测试异常')

下面是处理异常的代码:

        try:
            dic = self.Api.APIRun(APIname, **kwargs)
        except Exception as e:
            res['code'] = 1003
            res['msg'] = '调用 APIRun:%s 出错' % APIname
            res['data'] = e
            return res

报错:

Traceback (most recent call last):
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 202, in _get_response
    response = response.render()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\response.py", line 105, in render
    self.content = self.rendered_content
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\rest_framework\response.py", line 70, in rendered_content
    ret = renderer.render(self.data, accepted_media_type, context)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\rest_framework\renderers.py", line 103, in render
    allow_nan=not self.strict, separators=separators
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\rest_framework\utils\json.py", line 25, in dumps
    return json.dumps(*args, **kwargs)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 238, in dumps
    **kw).encode(obj)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\json\encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\rest_framework\utils\encoders.py", line 67, in default
    return super().default(obj)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\json\encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Exception is not JSON serializable

上面那个是重新封装的一个接口类,因为涉及到数据格式的处理,所以我就想如果格式处理错误,就抛出个异常,但是在原来的django里面用还好着呢,但是放到drf里面为什么会直接调用一个json.dumps网上很多都说要自定义下解析,然后再调用,但是这方法直接都不是我自己调用的,也走的不是异常模块,谁能帮忙看看怎么解决?


 
  • 写回答

1条回答 默认 最新

  • 我辈李想 Python领域优质创作者 2024-02-07 09:03
    关注

    因为e是一个对象,你需要str(e)转化为字符串

    评论

报告相同问题?

悬赏问题

  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统