Skip to content

Commit

Permalink
feature: Implement custom exceptions for Adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielanhaia committed Jun 23, 2020
1 parent 4fe92e3 commit 7b8a1c5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
19 changes: 9 additions & 10 deletions src/Adapter/Redis/RedisCircuitBreaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
*/
class RedisCircuitBreaker extends CircuitBreakerAdapter
Expand Down Expand Up @@ -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
{
Expand All @@ -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());
}
}

Expand All @@ -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
{
Expand All @@ -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());
}
}

Expand All @@ -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
{
Expand All @@ -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());
}
}

Expand All @@ -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
{
Expand All @@ -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());
}
}
}
18 changes: 18 additions & 0 deletions src/Exception/AdapterException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php


namespace GabrielAnhaia\LaravelCircuitBreaker\Exception;

use Throwable;

/**
* Class AdapterException
*
* @package GabrielAnhaia\LaravelCircuitBreaker\Exception
*
* @author Gabriel Anhaia <[email protected]>
*/
class AdapterException extends \Exception
{

}
21 changes: 11 additions & 10 deletions tests/Unit/Adapter/Redis/RedisCircuitBreakerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
Expand All @@ -78,7 +79,7 @@ public function testRedisErrorAddingNewFailureToAService()
/**
* Test success when opening the circuit.
*
* @throws \Exception
* @throws AdapterException
*/
public function testSuccessOpeningCircuit()
{
Expand All @@ -105,7 +106,7 @@ public function testSuccessOpeningCircuit()
/**
* Test a Redis error when trying to open the circuit.
*
* @throws \Exception
* @throws AdapterException
*/
public function testRedisErrorOpeningCircuit()
{
Expand All @@ -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);
Expand All @@ -141,7 +142,7 @@ public function testRedisErrorOpeningCircuit()
/**
* Test success when setting the circuit as half-open.
*
* @throws \Exception
* @throws AdapterException
*/
public function testSuccessHalfOpenCircuit()
{
Expand All @@ -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()
{
Expand All @@ -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);
Expand All @@ -204,7 +205,7 @@ public function testRedisErrorHalfOpenCircuit()
/**
* Test success closing the circuit.
*
* @throws \Exception
* @throws AdapterException
*/
public function testSuccessClosingCircuit()
{
Expand Down Expand Up @@ -242,7 +243,7 @@ public function testSuccessClosingCircuit()
/**
* Test a Redis error when trying to close the circuit.
*
* @throws \Exception
* @throws AdapterException
*/
public function testRedisErrorClosingCircuit()
{
Expand All @@ -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);
Expand Down

0 comments on commit 7b8a1c5

Please sign in to comment.