From 1bc21f6308d4a48f74368bb84c828eb9d69e349e Mon Sep 17 00:00:00 2001 From: Hilari Moragrega Date: Tue, 13 Jun 2017 15:19:16 +0200 Subject: [PATCH] Added silencer --- spec/BalancerBuilderSpec.php | 1 + .../ExceptionSilencerDecoratorSpec.php | 29 +++++++++++++ src/BalancerBuilder.php | 20 +++++++++ src/Decorator/ExceptionSilencerDecorator.php | 43 +++++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 spec/Decorator/ExceptionSilencerDecoratorSpec.php create mode 100644 src/Decorator/ExceptionSilencerDecorator.php diff --git a/spec/BalancerBuilderSpec.php b/spec/BalancerBuilderSpec.php index 7638afd..fda1007 100644 --- a/spec/BalancerBuilderSpec.php +++ b/spec/BalancerBuilderSpec.php @@ -19,6 +19,7 @@ function it_can_build_the_balancer(Monitor $monitor, LoggerInterface $logger) { $this->withLogger($logger)->shouldReturnAnInstanceOf(BalancerBuilder::class); $this->withMonitor($monitor, "foo")->shouldReturnAnInstanceOf(BalancerBuilder::class); + $this->withoutExceptions()->shouldReturnAnInstanceOf(BalancerBuilder::class); $this->create([ "feature_1" => ["foo" => 50, "off" => 50], "feature_2" => ["bar" => 20, "rab" => 40, "arb" => 40] diff --git a/spec/Decorator/ExceptionSilencerDecoratorSpec.php b/spec/Decorator/ExceptionSilencerDecoratorSpec.php new file mode 100644 index 0000000..da99537 --- /dev/null +++ b/spec/Decorator/ExceptionSilencerDecoratorSpec.php @@ -0,0 +1,29 @@ +beConstructedWith($balancer, $logger); + } + + function it_silences_exceptions(BalancerInterface $balancer, LoggerInterface $logger) + { + $exception = new OutOfBoundsException("feature foo is not set"); + $balancer->get("foo", 123456)->willThrow($exception); + + $this->get("foo", 123456)->shouldBe(""); + + $logger->error("Exception thrown balancing feature 'foo'", [ + "feature" => "foo", + "exception" => $exception, + ])->shouldHaveBeenCalled(); + } +} diff --git a/src/BalancerBuilder.php b/src/BalancerBuilder.php index fc78d7e..1ffb033 100644 --- a/src/BalancerBuilder.php +++ b/src/BalancerBuilder.php @@ -3,6 +3,7 @@ namespace Cmp\FeatureBalancer; use Cmp\FeatureBalancer\Config\Identifier; +use Cmp\FeatureBalancer\Decorator\ExceptionSilencerDecorator; use Cmp\FeatureBalancer\Decorator\LoggerDecorator; use Cmp\FeatureBalancer\Decorator\MonitoringDecorator; use Cmp\Monitoring\Monitor; @@ -31,6 +32,11 @@ class BalancerBuilder */ private $logLevel; + /** + * @var bool + */ + private $exceptions = true; + /** * @param array $config * @@ -49,6 +55,10 @@ public function create(array $config = []) $balancer = new LoggerDecorator($balancer, $this->logger, $this->logLevel); } + if (!$this->exceptions) { + $balancer = new ExceptionSilencerDecorator($balancer, $this->logger); + } + return $balancer; } @@ -80,6 +90,16 @@ public function withLogger(LoggerInterface $logger, $logLevel = LogLevel::INFO) return $this; } + /** + * @return $this + */ + public function withoutExceptions() + { + $this->exceptions = false; + + return $this; + } + /** * Add the features to the balancer * diff --git a/src/Decorator/ExceptionSilencerDecorator.php b/src/Decorator/ExceptionSilencerDecorator.php new file mode 100644 index 0000000..71c3624 --- /dev/null +++ b/src/Decorator/ExceptionSilencerDecorator.php @@ -0,0 +1,43 @@ +logger = $logger ?: new NullLogger(); + } + + /** + * {@inheritdoc} + */ + public function get($feature, $seed = null) + { + try { + return $this->balancer->get($feature, $seed); + } catch (BalancerException $exception) { + $this->logger->error("Exception thrown balancing feature '$feature'", [ + "feature" => $feature, + "exception" => $exception, + ]); + } + + return ""; + } +}