Skip to content

Commit

Permalink
Merge pull request #9 from kler/php7ErrorSupport
Browse files Browse the repository at this point in the history
Handle PHP 7 Error / Throwable
  • Loading branch information
jszobody authored May 12, 2020
2 parents 88b9a4a + 0ec1e78 commit 9509275
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Backoff.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ public function run($callback)
$this->wait($attempt);
try {
$result = call_user_func($callback);
} catch (\Throwable $e) {
if ($e instanceof \Error) {
$e = new Exception($e->getMessage(), $e->getCode(), $e);
}
$this->exceptions[] = $e;
$exception = $e;
} catch (Exception $e) {
$this->exceptions[] = $e;
$exception = $e;
Expand Down
18 changes: 18 additions & 0 deletions tests/BackoffTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace STS\Backoff;

use Exception;
use PHPUnit\Framework\TestCase;
use STS\Backoff\Strategies\ConstantStrategy;
use STS\Backoff\Strategies\ExponentialStrategy;
Expand Down Expand Up @@ -189,6 +190,23 @@ public function testFailedWorkReThrowsException()
});
}

public function testHandleErrorsPhp7()
{
$b = new Backoff(2, new ConstantStrategy(0));

$this->expectException(\Exception::class);
$this->expectExceptionMessage("Modulo by zero");

$b->run(function () {
if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
return 1 % 0;
} else {
// Handle version < 7
throw new Exception("Modulo by zero");
}
});
}

public function testAttempts()
{
$b = new Backoff(10, new ConstantStrategy(0));
Expand Down

0 comments on commit 9509275

Please # to comment.