Skip to content

Commit 6d164b7

Browse files
authored
Merge pull request #13 from utopia-php/feat-exception-handler
error handler for console loop
2 parents 5ff128a + 186d564 commit 6d164b7

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: php
22

33
php:
4-
- 7.4
54
- 8.0
65
- nightly
76

src/CLI/Console.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,16 +190,24 @@ static public function isInteractive(): bool
190190
/**
191191
* @param callable $callback
192192
* @param int $sleep in seconds
193+
* @param callable $onError
193194
*/
194-
static public function loop(callable $callback, $sleep = 1 /* 1 second */): void
195+
static public function loop(callable $callback, $sleep = 1 /* 1 second */, callable $onError = null): void
195196
{
196197
gc_enable();
197198

198199
$time = 0;
199200

200201
while (!connection_aborted() || PHP_SAPI == "cli") {
201-
202-
$callback();
202+
try {
203+
$callback();
204+
} catch(\Exception $e) {
205+
if($onError != null) {
206+
$onError($e);
207+
} else {
208+
throw $e;
209+
}
210+
}
203211

204212
sleep($sleep);
205213

0 commit comments

Comments
 (0)