Get exception handler based on debug
This commit is contained in:
parent
2a4a422722
commit
6a280e9ab5
1 changed files with 31 additions and 5 deletions
|
@ -2,7 +2,12 @@
|
||||||
|
|
||||||
namespace Core\Exceptions;
|
namespace Core\Exceptions;
|
||||||
|
|
||||||
|
use Core\Env\Env;
|
||||||
|
use Core\Http\Request;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
use Whoops\Handler\Handler;
|
||||||
|
use Whoops\Handler\JsonResponseHandler;
|
||||||
|
use Whoops\Handler\PlainTextHandler;
|
||||||
use Whoops\Handler\PrettyPageHandler;
|
use Whoops\Handler\PrettyPageHandler;
|
||||||
use Whoops\Run as Whoops;
|
use Whoops\Run as Whoops;
|
||||||
|
|
||||||
|
@ -18,13 +23,14 @@ class Exceptions
|
||||||
/**
|
/**
|
||||||
* Get exceptions handler instance
|
* Get exceptions handler instance
|
||||||
*
|
*
|
||||||
|
* @param \Core\Http\Request|null $request
|
||||||
* @return \Whoops\Run
|
* @return \Whoops\Run
|
||||||
*/
|
*/
|
||||||
public static function handler(): Whoops
|
public static function instance(Request|null $request = null): Whoops
|
||||||
{
|
{
|
||||||
if (!isset(self::$instance)) {
|
if (!isset(self::$instance)) {
|
||||||
$instance = new Whoops();
|
$instance = new Whoops();
|
||||||
$instance->pushHandler(new PrettyPageHandler());
|
$instance->pushHandler(self::handler($request));
|
||||||
|
|
||||||
self::$instance = $instance;
|
self::$instance = $instance;
|
||||||
}
|
}
|
||||||
|
@ -32,14 +38,34 @@ class Exceptions
|
||||||
return self::$instance;
|
return self::$instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get correct handler
|
||||||
|
*
|
||||||
|
* @param \Core\Http\Request|null $request
|
||||||
|
* @return \Whoops\Handler\Handler
|
||||||
|
*/
|
||||||
|
private static function handler(Request|null $request): Handler
|
||||||
|
{
|
||||||
|
if (Env::get('debug')) {
|
||||||
|
if ($request?->is('post')) {
|
||||||
|
return new JsonResponseHandler();
|
||||||
|
}
|
||||||
|
|
||||||
|
return new PrettyPageHandler();
|
||||||
|
}
|
||||||
|
|
||||||
|
return new PlainTextHandler();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Catch all exceptions
|
* Catch all exceptions
|
||||||
*
|
*
|
||||||
|
* @param \Core\Http\Request $request
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function catch(): void
|
public static function catch(Request $request): void
|
||||||
{
|
{
|
||||||
self::handler()->register();
|
self::instance($request)->register();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -50,6 +76,6 @@ class Exceptions
|
||||||
*/
|
*/
|
||||||
public static function catchOne(Throwable $exception): void
|
public static function catchOne(Throwable $exception): void
|
||||||
{
|
{
|
||||||
self::handler()->handleException($exception);
|
self::instance()->handleException($exception);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue