59 lines
1.8 KiB
PHP
59 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace WizdomNetworks\WizeWeb\Utils;
|
|
|
|
/**
|
|
* Error Handler Utility
|
|
*
|
|
* A utility for handling errors and exceptions globally.
|
|
*
|
|
* Automatically logs errors and exceptions using the Logger utility and displays generic error messages to users.
|
|
*/
|
|
class ErrorHandler
|
|
{
|
|
/**
|
|
* Registers the custom error and exception handlers.
|
|
*/
|
|
public static function register(): void
|
|
{
|
|
set_error_handler([self::class, 'handleError']);
|
|
set_exception_handler([self::class, 'handleException']);
|
|
Logger::info("ErrorHandler registered successfully.");
|
|
}
|
|
|
|
/**
|
|
* Custom error handler.
|
|
*
|
|
* Logs the error details and displays a generic error message.
|
|
*
|
|
* @param int $errno The error level.
|
|
* @param string $errstr The error message.
|
|
* @param string $errfile The file where the error occurred.
|
|
* @param int $errline The line number where the error occurred.
|
|
*/
|
|
public static function handleError(int $errno, string $errstr, string $errfile, int $errline): void
|
|
{
|
|
$message = "Error [$errno]: $errstr in $errfile on line $errline";
|
|
Logger::error($message);
|
|
http_response_code(500);
|
|
echo "An internal error occurred. Please try again later.";
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Custom exception handler.
|
|
*
|
|
* Logs the exception details and displays a generic error message.
|
|
*
|
|
* @param \Throwable $exception The exception object.
|
|
*/
|
|
public static function handleException(\Throwable $exception): void
|
|
{
|
|
$message = "Exception: " . $exception->getMessage() . " in " . $exception->getFile() . " on line " . $exception->getLine();
|
|
Logger::error($message);
|
|
http_response_code(500);
|
|
echo "An internal error occurred. Please try again later.";
|
|
exit;
|
|
}
|
|
}
|