Add Whoops as error handler

This commit is contained in:
Maarten 2019-12-26 01:16:03 +01:00
parent 032d8ff101
commit e516e6cbdf
4 changed files with 152 additions and 7 deletions

View file

@ -2,19 +2,54 @@
namespace Runtime\Exceptions;
use Whoops\Handler\JsonResponseHandler;
use Whoops\Handler\PlainTextHandler;
use Whoops\Run;
use Whoops\Handler\PrettyPageHandler;
use Whoops\Util\Misc;
class ExceptionHandler {
public static function make($abstract = null)
/**
* @var Run
*/
private static $handler;
/**
* @param null $abstract
* @param null $message
*/
public static function make($abstract = null, $message = null)
{
if(is_object($abstract) && $abstract instanceof \Exception) {
echo $abstract->getMessage();
die();
if(is_string($abstract)) {
$abstract = new $abstract($message);
}
echo 'Error: ' . $abstract;
if(is_subclass_of($abstract, 'Exception')) {
self::$handler->handleException($abstract);
}
die();
}
/**
* Register Whoops (Error handler)
*/
public static function register()
{
self::$handler = new Run();
self::$handler->pushHandler(new PrettyPageHandler());
if (Misc::isAjaxRequest()){
self::$handler->pushHandler(new JsonResponseHandler());
}
if (Misc::isCommandLine()){
self::$handler->pushHandler(new PlainTextHandler());
}
self::$handler->register();
}
}

View file

@ -16,6 +16,7 @@ class BootstrapFactory {
*/
public function handle()
{
ExceptionHandler::register();
Route::setDefaultNamespace('\App\Http\Controllers');
require_once('../routes/web.php');
@ -65,7 +66,7 @@ class BootstrapFactory {
}
}
ExceptionHandler::make(ClassNotFoundException::class);
ExceptionHandler::make(ClassNotFoundException::class, sprintf("Class '%s' not found", $abstract));
return null;
}