phasync
Frame / setExceptionHandler

Method

Frame::setExceptionHandler

Sends the failures of the frame's coroutines that nobody awaited to $handler, one by one, instead of having run() throw them.

Description#

public Frame::setExceptionHandler(
    ?Closure $handler
): void

A failure is known once the failed coroutine's Fiber is released, and at the latest when the run() ends. The handler runs in the event loop, outside any coroutine, so it must not wait. What it throws, run() throws, as a failure of the frame. What $fn throws is not passed to it: run() throws that. null removes the handler.

Parameters#

$handler ?Closure

Examples#

Example 1
phasync::run(function () use ($logger) {
    phasync::frame()->setExceptionHandler(fn (Throwable $e) => $logger->error($e->getMessage()));
    phasync::go(fn () => throw new RuntimeException('lost'));   // logged; run() goes on
});

See also#

  • phasync::runRuns $fn and returns its result once it and every coroutine started inside it have ended.