Reputation: 1436
i have custom exception for inserting data:
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)
exception is used in this insert function to 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
and i try to test the exception with self.repo._add(None)
but it shows error something like this:
FAILED tests/integration/test_repo.py::RatingRepoTest::test_add - TypeError: catching classes that do not inherit from BaseException is not allowed
Upvotes: 0
Views: 168
Reputation: 168966
Your syntax looks like it's a catch with a pattern match (which isn't a thing in Python).
Are you maybe looking for
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 Exception as e:
raise ErrorOnInsert(self.document_name) from e
Upvotes: 1