Skip to content

Commit d32ae1e

Browse files
committed
Added capability to halt retries by returning false from exception handler.
1 parent 5e182e6 commit d32ae1e

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ php:
99
- 5.5
1010
- 5.6
1111
- 7.0
12+
- 7.1
1213

1314
install:
1415
- alias composer=composer\ -n && composer selfupdate
1516
- composer validate
1617
- composer --prefer-source install
17-
- composer --prefer-source require satooshi/php-coveralls
1818

1919
script:
2020
- bin/test --coverage-clover=build/logs/clover.xml
2121

2222
after_success:
23+
- composer --prefer-source require satooshi/php-coveralls
2324
- vendor/bin/coveralls -v

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Retry
22
=====
33

44
[![Latest version][Version image]][Releases]
5+
[![Total downloads][Downloads image]][Downloads]
56
[![Build status][Build image]][Build]
67
[![Test coverage][Coverage image]][Coverage]
78
[![Code style][Style image]][Style]
@@ -19,15 +20,17 @@ Requirements
1920
Usage
2021
-----
2122

22-
The `retry` function retries an operation up to the specified number of times with an optional error handler and has
23-
the following signature.
23+
The `retry` function retries an operation up to the specified number of times with an optional exception handler.
24+
25+
If an exception handler is specified, it is called immediately before retrying the operation. If the handler returns
26+
`false`, the operation is not retried.
2427

2528
```
2629
retry(int $times, callable $operation, callable $onError = null);
2730
```
2831
* `$times`—Maximum number of times the operation may run.
2932
* `$operation`—Operation to run up to the specified number of times.
30-
* `$onError`—Optional. Error handler called immediately before retrying the operation.
33+
* `$onError`—Optional. Exception handler that receives the thrown exception as its first argument.
3134

3235
Note in the original library, `$times` specifies the number of *retries* and therefore the operation could run up to
3336
`$times + 1` times. In this version, `$times` specifies exactly the number of times the operation may run such that if
@@ -48,6 +51,8 @@ $response = retry(5, function () use ($url) {
4851

4952
[Releases]: https://github.com/ScriptFUSION/Retry/releases
5053
[Version image]: https://poser.pugx.org/scriptfusion/retry/v/stable "Latest version"
54+
[Downloads]: https://packagist.org/packages/scriptfusion/retry
55+
[Downloads image]: https://poser.pugx.org/scriptfusion/retry/downloads "Total downloads"
5156
[Build]: http://travis-ci.org/ScriptFUSION/Retry
5257
[Build image]: https://travis-ci.org/ScriptFUSION/Retry.svg "Build status"
5358
[Coverage]: https://coveralls.io/github/ScriptFUSION/Retry

src/retry.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ function retry($tries, callable $operation, callable $onError = null)
2929
throw new FailingTooHardException($attempts, $exception);
3030
}
3131

32-
$onError && $onError($exception);
32+
if ($onError && $onError($exception) === false) {
33+
return;
34+
}
3335

3436
goto beginning;
3537
}

test/RetryTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,24 @@ public function testErrorCallback()
9494

9595
self::assertInstanceOf(FailingTooHardException::class, $outerException);
9696
self::assertSame($tries, $invocations);
97-
self::assertSame(1, $errors);
97+
self::assertSame($tries - 1, $errors);
98+
}
99+
100+
public function testErrorCallbackHalt()
101+
{
102+
$invocations = 0;
103+
104+
try {
105+
\ScriptFUSION\Retry\retry($tries = 2, function () use (&$invocations) {
106+
++$invocations;
107+
108+
throw new \RuntimeException;
109+
}, function () {
110+
return false;
111+
});
112+
} catch (FailingTooHardException $exception) {
113+
}
114+
115+
self::assertSame(1, $invocations);
98116
}
99117
}

0 commit comments

Comments
 (0)