Update exception handler

This commit is contained in:
Maarten 2024-11-26 19:53:35 +01:00
parent 9b80b16380
commit eb3e310b0a
4 changed files with 59 additions and 28 deletions

View file

@ -2,7 +2,6 @@
namespace Core\Exceptions; namespace Core\Exceptions;
use Core\Http\Request;
use Throwable; use Throwable;
use Whoops\Handler\Handler; use Whoops\Handler\Handler;
use Whoops\Handler\JsonResponseHandler; use Whoops\Handler\JsonResponseHandler;
@ -13,30 +12,50 @@ use Whoops\Run as Whoops;
class ExceptionHandler class ExceptionHandler
{ {
/** /**
* Exceptions handler instance * Exceptions reporter instance
* *
* @var Whoops * @var Whoops
*/ */
private static Whoops $instance; private static Whoops $reporter;
/** /**
* Get exceptions handler instance * Exceptions handler instance
* *
* @param \Core\Http\Request|null $request * @var \Core\Exceptions\ExceptionHandler
* @return \Whoops\Run
*/ */
public static function instance(Request|null $request = null): Whoops private static ExceptionHandler $instance;
/**
* Get instance of exceptions handler
*
* @return \Core\Exceptions\ExceptionHandler
*/
public static function instance(): ExceptionHandler
{ {
if (!isset(self::$instance)) { if (!isset(self::$instance)) {
$instance = new Whoops(); self::$instance = new self();
$instance->pushHandler(self::handler($request));
self::$instance = $instance;
} }
return self::$instance; return self::$instance;
} }
/**
* Get exceptions reporter instance
*
* @return \Whoops\Run
*/
public static function reporter(): Whoops
{
if (!isset(self::$reporter)) {
$reporter = new Whoops();
$reporter->pushHandler(self::handler());
self::$reporter = $reporter;
}
return self::$reporter;
}
/** /**
* Get correct handler * Get correct handler
* *
@ -60,9 +79,9 @@ class ExceptionHandler
* *
* @return void * @return void
*/ */
public static function catch(): void public function register(): void
{ {
self::instance()->register(); self::reporter()->register();
} }
/** /**
@ -71,9 +90,9 @@ class ExceptionHandler
* @param \Throwable $exception * @param \Throwable $exception
* @return never * @return never
*/ */
public static function catchOne(Throwable $exception): never public function handle(Throwable $exception): never
{ {
self::instance()->handleException($exception); self::reporter()->handleException($exception);
exit(0); exit(0);
} }
@ -85,14 +104,14 @@ class ExceptionHandler
* @param string|null $message * @param string|null $message
* @return never * @return never
*/ */
public static function make(mixed $abstract = null, string|null $message = null): never public function make(mixed $abstract = null, string|null $message = null): never
{ {
if(is_string($abstract)) { if(is_string($abstract)) {
$abstract = app()->make($abstract, $message); $abstract = app()->make($abstract, $message);
} }
if(is_subclass_of($abstract, 'Exception')) { if(is_subclass_of($abstract, 'Exception')) {
self::catchOne($abstract); $this->handle($abstract);
} }
exit(0); exit(0);

View file

@ -24,8 +24,8 @@ class BootstrapFactory
// Create request // Create request
$this->request = app()->make(Request::class, [$_POST + $_FILES]); $this->request = app()->make(Request::class, [$_POST + $_FILES]);
// Capture all exceptions // Register exceptions handler for error reporting
ExceptionHandler::catch(); app()->make(ExceptionHandler::class)->register();
// Load routes // Load routes
require '../config/routes.php'; require '../config/routes.php';
@ -37,13 +37,13 @@ class BootstrapFactory
// Dispatch router // Dispatch router
app()->make(RouteDispatcher::class)->dispatch($this->request); app()->make(RouteDispatcher::class)->dispatch($this->request);
} catch (\Exception $e) { } catch (\Exception $e) {
ExceptionHandler::catchOne($e); exceptions()->handle($e);
} }
} }
/** /**
* @param string $abstract * @param string $abstract
* @param array $arguments * @param mixed $arguments
* @return mixed * @return mixed
*/ */
public function make(string $abstract, mixed $arguments = []): mixed public function make(string $abstract, mixed $arguments = []): mixed
@ -53,7 +53,7 @@ class BootstrapFactory
/** /**
* @param string $abstract * @param string $abstract
* @param array $arguments * @param mixed $arguments
* @return mixed|null * @return mixed|null
*/ */
private function resolve(string $abstract, mixed $arguments = []): mixed private function resolve(string $abstract, mixed $arguments = []): mixed
@ -63,11 +63,11 @@ class BootstrapFactory
$reflection = new \ReflectionClass($abstract); $reflection = new \ReflectionClass($abstract);
return $reflection->newInstanceArgs($arguments); return $reflection->newInstanceArgs($arguments);
} catch (\ReflectionException $e) { } catch (\ReflectionException $e) {
ExceptionHandler::catchOne($e); exceptions()->handle($e);
} }
} }
ExceptionHandler::make(ClassNotFoundException::class, sprintf("Class '%s' not found", $abstract)); exceptions()->make(ClassNotFoundException::class, sprintf("Class '%s' not found", $abstract));
} }
/** /**

View file

@ -2,6 +2,7 @@
use Core\Bootstrap; use Core\Bootstrap;
use Core\Env\Env; use Core\Env\Env;
use Core\Exceptions\ExceptionHandler;
use Core\Http\Request; use Core\Http\Request;
@ -11,10 +12,10 @@ if(!function_exists('app'))
* Make abstract of instance or get application instance * Make abstract of instance or get application instance
* *
* @param mixed $abstract * @param mixed $abstract
* @param array $arguments * @param mixed $arguments
* @return \Core\Factory\BootstrapFactory|mixed * @return \Core\Factory\BootstrapFactory|mixed
*/ */
function app(mixed $abstract = null, array $arguments = []): mixed function app(mixed $abstract = null, mixed $arguments = []): mixed
{ {
if (is_null($abstract)) { if (is_null($abstract)) {
return Bootstrap::getInstance(); return Bootstrap::getInstance();
@ -49,3 +50,15 @@ if (!function_exists('request')) {
return app()->request(); return app()->request();
} }
} }
if (!function_exists('exceptions')) {
/**
* Get error handler instance
*
* @return \Core\Exceptions\ExceptionHandler
*/
function exceptions(): ExceptionHandler
{
return ExceptionHandler::instance();
}
}

View file

@ -5,7 +5,6 @@ namespace Core\Routing;
use Core\Exceptions\ExceptionHandler; use Core\Exceptions\ExceptionHandler;
use Core\Exceptions\Exceptions\NotFoundHttpException; use Core\Exceptions\Exceptions\NotFoundHttpException;
use Core\Http\Request; use Core\Http\Request;
use Core\Http\View\Engine;
use Core\Http\View\Render; use Core\Http\View\Render;
use Exception; use Exception;
@ -136,7 +135,7 @@ class RouteDispatcher
private function handleException(Exception $e, int $statusCode, string $message): void private function handleException(Exception $e, int $statusCode, string $message): void
{ {
if (env('debug')) { if (env('debug')) {
ExceptionHandler::catchOne($e); exceptions()->handle($e);
} }
http_response_code($statusCode); http_response_code($statusCode);