diff --git a/includes/exceptions.php b/includes/exceptions.php new file mode 100644 index 00000000..aafad4fd --- /dev/null +++ b/includes/exceptions.php @@ -0,0 +1,10 @@ + + diff --git a/src/RaspAP/Exceptions/ExceptionHandler.php b/src/RaspAP/Exceptions/ExceptionHandler.php new file mode 100644 index 00000000..554be3a5 --- /dev/null +++ b/src/RaspAP/Exceptions/ExceptionHandler.php @@ -0,0 +1,57 @@ + + * @license https://github.com/raspap/raspap-webgui/blob/master/LICENSE + * @see + */ + +declare(strict_types=1); + +namespace RaspAP\Exceptions; + +class ExceptionHandler { + public static function handleException($exception) { + // Log the exception to a file or a service + $errorMessage = '[' . date('Y-m-d H:i:s') . '] ' . $exception->getMessage() . ' in ' . $exception->getFile() . ' on line ' . $exception->getLine() . PHP_EOL; + error_log($errorMessage, 3, 'error.log'); + + echo '

An error occured

'; + echo '

'.$errorMessage.'

'; + + //header('Location: error_page.php'); + } + + public static function handleFatalError() { + $error = error_get_last(); + if ($error !== null && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR])) { + // Log the fatal error + $errorMessage = '[' . date('Y-m-d H:i:s') . '] ' . $error['message'] . ' in ' . $error['file'] . ' on line ' . $error['line'] . PHP_EOL; + error_log($errorMessage, 3, 'error.log'); + + // Return HTTP 500 status header + //http_response_code(500); + + echo '

A Fatal error occured

'; + echo '

'.$errorMessage.'

'; + exit; + } + } + + public static function handleShutdown() { + $error = error_get_last(); + if ($error !== null && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR])) { + $errorMessage = '[' . date('Y-m-d H:i:s') . '] ' . $error['message'] . ' in ' . $error['file'] . ' on line ' . $error['line'] . PHP_EOL; + error_log($errorMessage, 3, 'error.log'); + + echo '

Executing shutdown

'; + echo '

'.$errorMessage.'

'; + + // header('Location: error_page.php'); + } + } +} +