ano-mr-site/lib/php/Catcher/Catcher.php

46 lines
1.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Catcher;
use Throwable;
/**
* Вызов обработчика ошибок с защитой от повторных исключений.
*
* @param callable $restorer Функция восстановления, принимающая исключения.
* @param Throwable ...$errors Исключения, переданные для обработки.
* @return mixed
* @throws Throwable
*/
function start_restorer(callable $restorer, Throwable ...$errors) {
try {
return $restorer(...$errors);
} catch (Throwable $e) {
error_log("Restorer error: " . $e->getMessage());
throw $e;
}
}
/**
* Оборачивает функцию и добавляет обработку ошибок.
*
* @param callable $func Основная функция.
* @param callable|null $restorer Обработчик ошибок (по умолчанию — пробрасывает исключение).
* @return callable
*/
function recovery(callable $func, callable $restorer = null): callable {
if ($restorer === null) {
$restorer = function (Throwable ...$errors) {
throw $errors[0]; // Пробрасываем первое исключение
};
}
return function (...$args) use ($func, $restorer) {
try {
return $func(...$args);
} catch (Throwable $e) {
return start_restorer($restorer, $e);
}
};
}
?>