Update exception handler
This commit is contained in:
parent
9b80b16380
commit
eb3e310b0a
4 changed files with 59 additions and 28 deletions
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace Core\Exceptions;
|
||||
|
||||
use Core\Http\Request;
|
||||
use Throwable;
|
||||
use Whoops\Handler\Handler;
|
||||
use Whoops\Handler\JsonResponseHandler;
|
||||
|
@ -13,30 +12,50 @@ use Whoops\Run as Whoops;
|
|||
class ExceptionHandler
|
||||
{
|
||||
/**
|
||||
* Exceptions handler instance
|
||||
* Exceptions reporter instance
|
||||
*
|
||||
* @var Whoops
|
||||
*/
|
||||
private static Whoops $instance;
|
||||
private static Whoops $reporter;
|
||||
|
||||
/**
|
||||
* Get exceptions handler instance
|
||||
* Exceptions handler instance
|
||||
*
|
||||
* @param \Core\Http\Request|null $request
|
||||
* @return \Whoops\Run
|
||||
* @var \Core\Exceptions\ExceptionHandler
|
||||
*/
|
||||
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)) {
|
||||
$instance = new Whoops();
|
||||
$instance->pushHandler(self::handler($request));
|
||||
|
||||
self::$instance = $instance;
|
||||
self::$instance = new self();
|
||||
}
|
||||
|
||||
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
|
||||
*
|
||||
|
@ -60,9 +79,9 @@ class ExceptionHandler
|
|||
*
|
||||
* @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
|
||||
* @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);
|
||||
}
|
||||
|
@ -85,14 +104,14 @@ class ExceptionHandler
|
|||
* @param string|null $message
|
||||
* @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)) {
|
||||
$abstract = app()->make($abstract, $message);
|
||||
}
|
||||
|
||||
if(is_subclass_of($abstract, 'Exception')) {
|
||||
self::catchOne($abstract);
|
||||
$this->handle($abstract);
|
||||
}
|
||||
|
||||
exit(0);
|
||||
|
|
|
@ -24,8 +24,8 @@ class BootstrapFactory
|
|||
// Create request
|
||||
$this->request = app()->make(Request::class, [$_POST + $_FILES]);
|
||||
|
||||
// Capture all exceptions
|
||||
ExceptionHandler::catch();
|
||||
// Register exceptions handler for error reporting
|
||||
app()->make(ExceptionHandler::class)->register();
|
||||
|
||||
// Load routes
|
||||
require '../config/routes.php';
|
||||
|
@ -37,13 +37,13 @@ class BootstrapFactory
|
|||
// Dispatch router
|
||||
app()->make(RouteDispatcher::class)->dispatch($this->request);
|
||||
} catch (\Exception $e) {
|
||||
ExceptionHandler::catchOne($e);
|
||||
exceptions()->handle($e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $abstract
|
||||
* @param array $arguments
|
||||
* @param mixed $arguments
|
||||
* @return mixed
|
||||
*/
|
||||
public function make(string $abstract, mixed $arguments = []): mixed
|
||||
|
@ -53,7 +53,7 @@ class BootstrapFactory
|
|||
|
||||
/**
|
||||
* @param string $abstract
|
||||
* @param array $arguments
|
||||
* @param mixed $arguments
|
||||
* @return mixed|null
|
||||
*/
|
||||
private function resolve(string $abstract, mixed $arguments = []): mixed
|
||||
|
@ -63,11 +63,11 @@ class BootstrapFactory
|
|||
$reflection = new \ReflectionClass($abstract);
|
||||
return $reflection->newInstanceArgs($arguments);
|
||||
} 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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
use Core\Bootstrap;
|
||||
use Core\Env\Env;
|
||||
use Core\Exceptions\ExceptionHandler;
|
||||
use Core\Http\Request;
|
||||
|
||||
|
||||
|
@ -11,10 +12,10 @@ if(!function_exists('app'))
|
|||
* Make abstract of instance or get application instance
|
||||
*
|
||||
* @param mixed $abstract
|
||||
* @param array $arguments
|
||||
* @param mixed $arguments
|
||||
* @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)) {
|
||||
return Bootstrap::getInstance();
|
||||
|
@ -49,3 +50,15 @@ if (!function_exists('request')) {
|
|||
return app()->request();
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('exceptions')) {
|
||||
/**
|
||||
* Get error handler instance
|
||||
*
|
||||
* @return \Core\Exceptions\ExceptionHandler
|
||||
*/
|
||||
function exceptions(): ExceptionHandler
|
||||
{
|
||||
return ExceptionHandler::instance();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@ namespace Core\Routing;
|
|||
use Core\Exceptions\ExceptionHandler;
|
||||
use Core\Exceptions\Exceptions\NotFoundHttpException;
|
||||
use Core\Http\Request;
|
||||
use Core\Http\View\Engine;
|
||||
use Core\Http\View\Render;
|
||||
use Exception;
|
||||
|
||||
|
@ -136,7 +135,7 @@ class RouteDispatcher
|
|||
private function handleException(Exception $e, int $statusCode, string $message): void
|
||||
{
|
||||
if (env('debug')) {
|
||||
ExceptionHandler::catchOne($e);
|
||||
exceptions()->handle($e);
|
||||
}
|
||||
|
||||
http_response_code($statusCode);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue