canonical/pylxd

TypeError: catching classes that do not inherit from BaseException is not allowed

dmzoneill opened this issue · 1 comments

Just doing some unit testing here with python 3.6

Triggering the exception with monkey patch

        def func():
            containers_property_mock = mock.MagicMock()
            containers_property_mock.get.side_effect = pylxd.exceptions.NotFound('not found')

            mock_pyxld_client = mock.MagicMock()
            type(mock_pyxld_client).containers = mock.PropertyMock(return_value=containers_property_mock)
            return mock_pyxld_client

        monkeypatch.setattr("lib_vpn_customer.pylxd.Client", func)

    @property
    def container_exists(self):
        """Return the container instance, if found."""
        if self.lxd_client is None:
            self.lxd_client = pylxd.Client()
        try:
            self.lxd_container = self.lxd_client.containers.get(self.appname)
            pp = pprint.PrettyPrinter(indent=4)
            pp.pprint(self.lxd_container)
            return True
>       except pylxd.exceptions.NotFound:
E       TypeError: catching classes that do not inherit from BaseException is not allowed

Either I'm missing something or need to modify the exception hierarchy to extend from base exception.

        except:
            # TypeError: catching classes that do not inherit from BaseException is not allowed
            hookenv.log('Could not find container {}'.format(sys.exc_info()[0]), level=hookenv.ERROR)
            return False

workaround for now