From 7b8a1c5cb779c29a75b4bdda7b9dc28217fc5878 Mon Sep 17 00:00:00 2001 From: Gabriel Anhaia Date: Tue, 23 Jun 2020 08:47:30 -0300 Subject: [PATCH] feature: Implement custom exceptions for Adapters --- src/Adapter/Redis/RedisCircuitBreaker.php | 19 ++++++++--------- src/Exception/AdapterException.php | 18 ++++++++++++++++ .../Adapter/Redis/RedisCircuitBreakerTest.php | 21 ++++++++++--------- 3 files changed, 38 insertions(+), 20 deletions(-) create mode 100644 src/Exception/AdapterException.php diff --git a/src/Adapter/Redis/RedisCircuitBreaker.php b/src/Adapter/Redis/RedisCircuitBreaker.php index 6e731bd..64567af 100644 --- a/src/Adapter/Redis/RedisCircuitBreaker.php +++ b/src/Adapter/Redis/RedisCircuitBreaker.php @@ -5,14 +5,13 @@ use GabrielAnhaia\LaravelCircuitBreaker\CircuitState; use GabrielAnhaia\LaravelCircuitBreaker\Contract\CircuitBreakerAdapter; +use GabrielAnhaia\LaravelCircuitBreaker\Exception\AdapterException; /** * Class RedisCircuitBreaker * * @package GabrielAnhaia\LaravelCircuitBreaker\Adapter * - * TODO: Create custom exceptions for the Adapter layer. - * * @author Gabriel Anhaia */ class RedisCircuitBreaker extends CircuitBreakerAdapter @@ -64,7 +63,7 @@ public function getState(string $serviceName): CircuitState * @param string $serviceName Service name to increment a failure. * @param int $timeWindow Time for each error be stored. * - * @throws \Exception + * @throws AdapterException */ public function addFailure(string $serviceName, int $timeWindow): void { @@ -73,7 +72,7 @@ public function addFailure(string $serviceName, int $timeWindow): void $dataInserted = $this->redis->set($keyTotalFailures, $timeWindow); if ($dataInserted === false) { - throw new \Exception($this->redis->getLastError()); + throw new AdapterException($this->redis->getLastError()); } } @@ -99,7 +98,7 @@ public function getTotalFailures(string $serviceName): int * @param string $serviceName Service name of the circuit to be opened. * @param int $timeOpen Time in second that the circuit will stay open. * - * @throws \Exception + * @throws AdapterException */ public function openCircuit(string $serviceName, int $timeOpen): void { @@ -108,7 +107,7 @@ public function openCircuit(string $serviceName, int $timeOpen): void $dataInserted = $this->redis->set($key, $timeOpen); if ($dataInserted === false) { - throw new \Exception($this->redis->getLastError()); + throw new AdapterException($this->redis->getLastError()); } } @@ -117,7 +116,7 @@ public function openCircuit(string $serviceName, int $timeOpen): void * * @param string $serviceName * - * @throws \Exception + * @throws AdapterException */ public function closeCircuit(string $serviceName): void { @@ -128,7 +127,7 @@ public function closeCircuit(string $serviceName): void $dataDeleted = $this->redis->delete($openCircuitKey, $halfOpenCircuitKey, $failuresByServiceKey); if ($dataDeleted === false) { - throw new \Exception($this->redis->getLastError()); + throw new AdapterException($this->redis->getLastError()); } } @@ -138,7 +137,7 @@ public function closeCircuit(string $serviceName): void * @param string $serviceName Service name * @param int $timeOpen Time that the circuit will be half-open. * - * @throws \Exception + * @throws AdapterException */ public function setCircuitHalfOpen(string $serviceName, int $timeOpen): void { @@ -147,7 +146,7 @@ public function setCircuitHalfOpen(string $serviceName, int $timeOpen): void $dataInserted = $this->redis->set($key, $timeOpen); if ($dataInserted === false) { - throw new \Exception($this->redis->getLastError()); + throw new AdapterException($this->redis->getLastError()); } } } \ No newline at end of file diff --git a/src/Exception/AdapterException.php b/src/Exception/AdapterException.php new file mode 100644 index 0000000..a8bbfc8 --- /dev/null +++ b/src/Exception/AdapterException.php @@ -0,0 +1,18 @@ + + */ +class AdapterException extends \Exception +{ + +} \ No newline at end of file diff --git a/tests/Unit/Adapter/Redis/RedisCircuitBreakerTest.php b/tests/Unit/Adapter/Redis/RedisCircuitBreakerTest.php index b66a538..ed63407 100644 --- a/tests/Unit/Adapter/Redis/RedisCircuitBreakerTest.php +++ b/tests/Unit/Adapter/Redis/RedisCircuitBreakerTest.php @@ -5,6 +5,7 @@ use GabrielAnhaia\LaravelCircuitBreaker\Adapter\Redis\KeyHelper; use GabrielAnhaia\LaravelCircuitBreaker\Adapter\Redis\RedisCircuitBreaker; use GabrielAnhaia\LaravelCircuitBreaker\CircuitState; +use GabrielAnhaia\LaravelCircuitBreaker\Exception\AdapterException; use Tests\TestCase; /** @@ -51,7 +52,7 @@ public function testRedisErrorAddingNewFailureToAService() $keyFailure = 'circuit_breaker:service:total_failures:11111'; $redisErrorMessage = 'UNEXPECTED_ERROR_MESSAGE'; - $this->expectException(\Exception::class); + $this->expectException(AdapterException::class); $this->expectExceptionMessage($redisErrorMessage); $keyHelperMock = \Mockery::mock(KeyHelper::class); @@ -78,7 +79,7 @@ public function testRedisErrorAddingNewFailureToAService() /** * Test success when opening the circuit. * - * @throws \Exception + * @throws AdapterException */ public function testSuccessOpeningCircuit() { @@ -105,7 +106,7 @@ public function testSuccessOpeningCircuit() /** * Test a Redis error when trying to open the circuit. * - * @throws \Exception + * @throws AdapterException */ public function testRedisErrorOpeningCircuit() { @@ -114,7 +115,7 @@ public function testRedisErrorOpeningCircuit() $keyCircuitOpen = 'circuit_breaker:service:open'; $redisErrorMessage = 'UNEXPECTED_ERROR_MESSAGE'; - $this->expectException(\Exception::class); + $this->expectException(AdapterException::class); $this->expectExceptionMessage($redisErrorMessage); $keyHelperMock = \Mockery::mock(KeyHelper::class); @@ -141,7 +142,7 @@ public function testRedisErrorOpeningCircuit() /** * Test success when setting the circuit as half-open. * - * @throws \Exception + * @throws AdapterException */ public function testSuccessHalfOpenCircuit() { @@ -168,7 +169,7 @@ public function testSuccessHalfOpenCircuit() /** * Test a Redis error when trying to set the circuit as half-open. * - * @throws \Exception + * @throws AdapterException */ public function testRedisErrorHalfOpenCircuit() { @@ -177,7 +178,7 @@ public function testRedisErrorHalfOpenCircuit() $keyCircuitHalfOpen = 'circuit_breaker:service:half_open'; $redisErrorMessage = 'UNEXPECTED_ERROR_MESSAGE'; - $this->expectException(\Exception::class); + $this->expectException(AdapterException::class); $this->expectExceptionMessage($redisErrorMessage); $keyHelperMock = \Mockery::mock(KeyHelper::class); @@ -204,7 +205,7 @@ public function testRedisErrorHalfOpenCircuit() /** * Test success closing the circuit. * - * @throws \Exception + * @throws AdapterException */ public function testSuccessClosingCircuit() { @@ -242,7 +243,7 @@ public function testSuccessClosingCircuit() /** * Test a Redis error when trying to close the circuit. * - * @throws \Exception + * @throws AdapterException */ public function testRedisErrorClosingCircuit() { @@ -253,7 +254,7 @@ public function testRedisErrorClosingCircuit() $redisErrorMessage = 'UNEXPECTED_ERROR_MESSAGE'; - $this->expectException(\Exception::class); + $this->expectException(AdapterException::class); $this->expectExceptionMessage($redisErrorMessage); $keyHelperMock = \Mockery::mock(KeyHelper::class);