danielfm / pybreaker

Python implementation of the Circuit Breaker pattern.
BSD 3-Clause "New" or "Revised" License
512 stars 74 forks source link

Implement _exceptions list #20

Open pythons1980 opened 7 years ago

pythons1980 commented 7 years ago

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.

sebastiandev commented 5 years ago

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

yairm210 commented 2 years ago

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?