Skip to content

Instantly share code, notes, and snippets.

@sroze
Last active Apr 3, 2018
Embed
What would you like to do?
<?php
// Expected output:
// Got exception do nothing
// Got message test2
// Got message test3
// Actual output:
// Got exception do nothing
// Got message test3
class Message {
public $str;
public function __construct($str) {
$this->str = $str;
}
}
function receive(): iterable
{
$messages = [new Message('test1'), new Message('test2'), new Message('test3')];
while (true) {
if (null === $message = array_shift($messages)) {
break;
}
try {
yield $message;
} catch (\Throwable $e) {
echo 'Got exception do nothing'.PHP_EOL;
}
}
}
$throwed = false;
$handler = function ($t) use (&$throwed) {
if (false === $throwed) {
$throwed = true;
throw new \Exception('Fail');
}
echo sprintf('Got message %s', $t->str).PHP_EOL;
};
$iterator = receive();
foreach ($iterator as $message) {
try {
$handler($message);
} catch (\Throwable $e) {
$iterator->throw($e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment