-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Implement custom exceptions for Adapters
- Loading branch information
1 parent
4fe92e3
commit 7b8a1c5
Showing
3 changed files
with
38 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters