0

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

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.