Lines
91.66%
11 / 12
Methods
75.00%
3 / 4
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| install | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | ||
| restorePrevious | 80.00% | 4 / 5 | 0.00% | 0 / 1 | 2.03 | ||
| logError | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| 12 | final class LoopErrorLogger | |
| 13 | { | |
| 14 | /** @var (Closure(Throwable): void)|null */ | |
| 15 | private ?Closure $previous = null; | |
| 16 | ||
| 17 | private bool $installed = false; | |
| 18 | ||
| 19 | public function __construct(private readonly LoggerInterface $logger) {} | |
| 20 | ||
| 21 | public function install(): void | |
| 22 | { | |
| 23 | if ($this->installed) { | |
| 24 | return; | |
| 25 | } | |
| 26 | ||
| 27 | $this->previous = EventLoop::getErrorHandler(); | |
| 28 | EventLoop::setErrorHandler($this->logError(...)); | |
| 29 | $this->installed = true; | |
| 30 | } | |
| 31 | ||
| 32 | public function restorePrevious(): void | |
| 33 | { | |
| 34 | if (!$this->installed) { | |
| 35 | return; | |
| 36 | } | |
| 37 | ||
| 38 | EventLoop::setErrorHandler($this->previous); | |
| 39 | $this->previous = null; | |
| 40 | $this->installed = false; | |
| 41 | } | |
| 42 | ||
| 43 | private function logError(Throwable $error): void | |
| 44 | { | |
| 45 | $this->logger->critical('Unhandled error in the event loop', ['exception' => $error]); | |
| 46 | } | |
| 47 | } |