Scope::setChildScopeExceptionHandler

(PHP 8.6+, True Async 1.0)

public function setChildScopeExceptionHandler(callable $exceptionHandler): void

Establece un manejador de excepciones para las excepciones lanzadas en los ámbitos hijos. Cuando un ámbito hijo termina con un error, se llama a este manejador, evitando que la excepción se propague al ámbito padre.

Parámetros

exceptionHandler — la función de manejo de excepciones para los ámbitos hijos. Acepta un \Throwable como argumento.

Valor de retorno

No se devuelve ningún valor.

Ejemplos

Ejemplo #1 Capturar errores de ámbitos hijos

<?php

use Async\Scope;

$parentScope = new Scope();

$parentScope->setChildScopeExceptionHandler(function(\Throwable $e) {
    error_log("Error in child scope: " . $e->getMessage());
});

$childScope = Scope::inherit($parentScope);

$childScope->spawn(function() {
    throw new \RuntimeException("Child scope error");
});

$childScope->awaitCompletion();
// Error handled, does not propagate to $parentScope

Ejemplo #2 Aislar errores entre módulos

<?php

use Async\Scope;

$appScope = new Scope();

$appScope->setChildScopeExceptionHandler(function(\Throwable $e) {
    error_log("[App] Module error: " . $e->getMessage());
});

// Each module in its own scope
$authScope = Scope::inherit($appScope);
$cacheScope = Scope::inherit($appScope);

$authScope->spawn(function() {
    // An error here will not affect $cacheScope
    throw new \RuntimeException("Auth failed");
});

$cacheScope->spawn(function() {
    echo "Cache is working fine\n";
});

$appScope->awaitCompletion();

Ver también