Add error handler for exceptions

This commit is contained in:
Sebastian Meyer 2024-01-24 22:44:31 +01:00
parent 3f633ef11e
commit 7311da4b22
2 changed files with 58 additions and 1 deletions

View File

@ -13,7 +13,8 @@
"StrictList", "StrictList",
"StrictQueue", "StrictQueue",
"StrictStack", "StrictStack",
"ThrowErrorException" "ThrowErrorException",
"TriggerExceptionError"
], ],
"homepage": "https://github.com/opencultureconsulting/php-basics", "homepage": "https://github.com/opencultureconsulting/php-basics",
"readme": "README.md", "readme": "README.md",

View File

@ -0,0 +1,56 @@
<?php
/**
* PHP Basics
*
* Copyright (C) 2024 Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace OCC\Basics\ErrorHandlers;
use Throwable;
/**
* Trigger errors for uncaught exceptions.
*
* Usage: set_exception_handler(new TriggerExceptionError());
*
* @author Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
* @package Basics\ErrorHandlers
*/
class TriggerExceptionError
{
/**
* Convert an uncaught exception into an PHP error.
*
* @param Throwable $exception The exception
*
* @return void
*/
public function __invoke(Throwable $exception): void
{
$message = sprintf(
'Uncaught Exception [%d] in file %s on line %d: %s',
$exception->getCode(),
$exception->getFile(),
$exception->getLine(),
$exception->getMessage()
);
trigger_error($message, E_USER_ERROR);
}
}