2023-11-12 15:50:19 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Useful PHP Basics
|
|
|
|
* Copyright (C) 2023 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);
|
|
|
|
|
2023-12-16 21:15:47 +01:00
|
|
|
namespace OCC\Basics\ErrorHandlers;
|
2023-11-12 15:50:19 +01:00
|
|
|
|
|
|
|
use ErrorException;
|
|
|
|
|
|
|
|
/**
|
2023-12-16 21:15:47 +01:00
|
|
|
* Throw internal errors as exceptions.
|
2023-11-15 12:01:21 +01:00
|
|
|
*
|
2023-12-16 21:15:47 +01:00
|
|
|
* Usage: set_error_handler(new ThrowErrorException());
|
2023-11-12 15:50:19 +01:00
|
|
|
*
|
|
|
|
* @author Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
|
|
|
|
* @package opencultureconsulting/basics
|
|
|
|
*/
|
2023-12-16 21:15:47 +01:00
|
|
|
class ThrowErrorException
|
2023-11-12 15:50:19 +01:00
|
|
|
{
|
2023-12-16 21:15:47 +01:00
|
|
|
/**
|
|
|
|
* Converts an internal PHP error into an ErrorException.
|
|
|
|
*
|
|
|
|
* @param int $severity The severity of the error
|
|
|
|
* @param string $message The error message
|
|
|
|
* @param ?string $file The name of the file the error was raised in
|
|
|
|
* @param ?int $line The line number the error was raised in
|
|
|
|
*
|
|
|
|
* @return bool Always returns FALSE when not throwing an exception
|
|
|
|
*
|
|
|
|
* @throws ErrorException
|
|
|
|
*/
|
|
|
|
public function __invoke(int $severity = E_USER_ERROR, string $message = '', ?string $file = null, ?int $line = null): bool
|
|
|
|
{
|
|
|
|
if ((error_reporting() & $severity) > 0) {
|
|
|
|
throw new ErrorException($message, 0, $severity, $file, $line);
|
|
|
|
}
|
|
|
|
return false;
|
2023-11-12 15:50:19 +01:00
|
|
|
}
|
|
|
|
}
|