80 lines
3 KiB
PHP
80 lines
3 KiB
PHP
<?php
|
|
// Exceptions.php
|
|
// Created: 2021-04-30
|
|
// Updated: 2021-05-12
|
|
|
|
namespace Index;
|
|
|
|
use ErrorException;
|
|
use Throwable;
|
|
|
|
/**
|
|
* Provides handling for uncaught exceptions and errors.
|
|
*/
|
|
final class Exceptions {
|
|
/**
|
|
* Convert errors to ErrorExceptions.
|
|
*
|
|
* Automatically invoked by inclusion of index.php into your project unless the constant <code>NDX_LEAVE_ERRORS</code> is defined beforehand.
|
|
* This is not recommended as it may cause undefined behaviour in some classes.
|
|
* This will also make error suppression not work, luckily you've not been using that since PHP 5. Right? Right?!
|
|
* Besides, this makes it possible to try..catch errors.
|
|
*/
|
|
public static function convertErrors(): void {
|
|
self::restoreErrors();
|
|
set_error_handler([self::class, 'handleError'], -1);
|
|
}
|
|
|
|
/**
|
|
* Restores error handling to the default PHP state.
|
|
*/
|
|
public static function restoreErrors(): void {
|
|
restore_error_handler();
|
|
}
|
|
|
|
/**
|
|
* Handle uncaught exceptions.
|
|
*
|
|
* Automatically invoked by inclusion of index.php into your project unless the constant <code>NDX_LEAVE_EXCEPTIONS</code> is defined.
|
|
*/
|
|
public static function handleExceptions(): void {
|
|
self::restoreExceptions();
|
|
//set_exception_handler([self::class, 'handleException']);
|
|
}
|
|
|
|
/**
|
|
* Restores uncaught exception handling to the default PHP state.
|
|
*/
|
|
public static function restoreExceptions(): void {
|
|
restore_exception_handler();
|
|
}
|
|
|
|
/**
|
|
* Converts errors to ErrorExceptions.
|
|
*
|
|
* Paramater documentation is copied from the set_error_handler page on php.net
|
|
*
|
|
* @see https://www.php.net/manual/en/function.set-error-handler.php
|
|
* @param int $errno The first parameter, errno, will be passed the level of the error raised, as an integer.
|
|
* @param string $errstr The second parameter, errstr, will be passed the error message, as a string.
|
|
* @param string $errfile If the callback accepts a third parameter, errfile, it will be passed the filename that the error was raised in, as a string.
|
|
* @param int $errline If the callback accepts a fourth parameter, errline, it will be passed the line number where the error was raised, as an integer.
|
|
* @throws ErrorException An ErrorException with the provided parameters.
|
|
* @return bool if this were false the PHP error handler would continue, but returning is never reached.
|
|
*/
|
|
public static function handleError(int $errno, string $errstr, string $errfile, int $errline): bool {
|
|
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
|
|
}
|
|
|
|
/**
|
|
* Handles uncaught exceptions.
|
|
*
|
|
* @see https://www.php.net/manual/en/function.set-exception-handler.php
|
|
* @param ?Throwable $ex Uncaught Throwable to handle. May be null to reset state(?) apparently.
|
|
*/
|
|
public static function handleException(?Throwable $ex): void {
|
|
if($ex === null)
|
|
return;
|
|
}
|
|
}
|