pybreaker icon indicating copy to clipboard operation
pybreaker copied to clipboard

Implement _exceptions list

Open pythons1980 opened this issue 8 years ago • 2 comments

Hi All,

it would be nice if function is_system_error can work with self._exceptions as well. I am about to implement ES circuit breaker for just one exception (ES BROKEN CONNECTION) so that I have to create my own Breaker (subclassing CircuitBreaker) and rewrite function is_system_error instead of just define one exception in the new list self._exceptions.

Regards, Vojtech.

pythons1980 avatar Jun 16 '17 13:06 pythons1980

Yes, I needed something similar and ended up using a callable that check that the error is of the type i want. But that functionality is only in the latest master, not in a release

sebastiandev avatar Mar 19 '19 15:03 sebastiandev

Now that some time has passed, it's simple to do this as sebastiandev has said

Including a test for this here, modeled after the existing tests in tests.py:


    def test_allow_whitelist(self):
        """CircuitBreaker: it should allow the user to add a predicate function to determine excluded exceptions.
        """
        acceptableExceptions = [NotImplementedError]
        isUnacceptableException = lambda e: type(e) not in acceptableExceptions

        self.breaker = CircuitBreaker(exclude=[isUnacceptableException])


        def throwsAcceptableError(): raise NotImplementedError
        def throwsUnacceptableError(): raise LookupError

        self.assertRaises(NotImplementedError, self.breaker.call, throwsAcceptableError)
        self.assertEqual(1, self.breaker.fail_counter)

        self.assertRaises(LookupError, self.breaker.call, throwsUnacceptableError)
        self.assertEqual(0, self.breaker.fail_counter)

Also this issue seems to be a copy of #5, so probably one of them should be closed?

yairm210 avatar Mar 14 '22 09:03 yairm210