From 4b7ee9b876509258350ebb655d0c081eefc2fb7c Mon Sep 17 00:00:00 2001 From: Sebastian Meyer Date: Sun, 12 Nov 2023 15:50:19 +0100 Subject: [PATCH] Add error handler to convert to exceptions --- src/Functions/throwError.php | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/Functions/throwError.php diff --git a/src/Functions/throwError.php b/src/Functions/throwError.php new file mode 100644 index 0000000..debfe54 --- /dev/null +++ b/src/Functions/throwError.php @@ -0,0 +1,49 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +declare(strict_types=1); + +namespace OCC\Basics\Functions; + +use ErrorException; + +/** + * Handles an internal PHP error and throws it as ErrorException. + * @example set_error_handler('\\OCC\\Basics\\Functions\\throwError', E_ALL); + * + * @author Sebastian Meyer + * @package opencultureconsulting/basics + * + * @param int $severity The severity of the error + * @param string $message The error message + * @param ?string $filename The name of the file the error was raised in + * @param ?int $line The number of the line the error was raised in + * + * @return bool Always returns FALSE when not throwing an exception + * + * @throws \ErrorException + */ +function throwError(int $severity = E_ALL, string $message = '', ?string $filename = null, ?int $line = null): bool +{ + if (error_reporting() & $severity) { + throw new ErrorException($message, 0, $severity, $filename, $line); + } + return false; +}