【发布时间】:2021-03-06 23:48:56
【问题描述】:
我有插入数据的自定义例外:
class ErrorOnInsert(BaseException):
"""Exception raised for errors in insert data to db
Attributes:
resource -- resource name
message -- explanation of the error
"""
def __init__(self, resource: str):
self.message = 'Failed to insert data for {}!'.format(resource)
super().__init__(self.message)
这个插入函数中使用了异常到mongo:
def _add(self, data: RunnerRating):
try:
dict_data = data.as_dict()
self.collection[self.document_name].insert_one(
dict_data)
self.list_data.add(data)
except ErrorOnInsert(self.document_name) as e:
raise e
我尝试使用self.repo._add(None) 测试异常,但它显示如下错误:
失败的测试/集成/test_repo.py::RatingRepoTest::test_add - TypeError:捕获不从 BaseException 继承的类是 不允许
【问题讨论】: