Refactored ExceptionHandler
parent
6eea072376
commit
25e434bce4
@ -1,119 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Exceptions;
|
||||
|
||||
use ErrorException;
|
||||
use Throwable;
|
||||
|
||||
class BasicHandler extends Handler
|
||||
{
|
||||
/**
|
||||
* Activate the error handler
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
set_error_handler([$this, 'errorHandler']);
|
||||
set_exception_handler([$this, 'exceptionHandler']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $number
|
||||
* @param string $message
|
||||
* @param string $file
|
||||
* @param int $line
|
||||
*/
|
||||
public function errorHandler($number, $message, $file, $line)
|
||||
{
|
||||
$exception = new ErrorException($message, 0, $number, $file, $line);
|
||||
$this->exceptionHandler($exception);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Throwable $e
|
||||
*/
|
||||
public function exceptionHandler($e)
|
||||
{
|
||||
$this->handle(
|
||||
$e->getCode(),
|
||||
get_class($e) . ': ' . $e->getMessage(),
|
||||
$e->getFile(),
|
||||
$e->getLine(),
|
||||
['exception' => $e]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $number
|
||||
* @param string $string
|
||||
* @param string $file
|
||||
* @param int $line
|
||||
* @param array $context
|
||||
* @param array $trace
|
||||
*/
|
||||
protected function handle($number, $string, $file, $line, $context = [], $trace = [])
|
||||
{
|
||||
error_log(sprintf('Exception: Number: %s, String: %s, File: %s:%u, Context: %s',
|
||||
$number,
|
||||
$string,
|
||||
$file,
|
||||
$line,
|
||||
json_encode($context)
|
||||
));
|
||||
|
||||
$file = $this->stripBasePath($file);
|
||||
|
||||
if ($this->environment == self::ENV_DEVELOPMENT) {
|
||||
echo '<pre style="background-color:#333;color:#ccc;z-index:1000;position:fixed;bottom:1em;padding:1em;width:97%;max-height: 90%;overflow-y:auto;">';
|
||||
echo sprintf('%s: (%s)' . PHP_EOL, ucfirst($type), $number);
|
||||
var_export([
|
||||
'string' => $string,
|
||||
'file' => $file . ':' . $line,
|
||||
'context' => $context,
|
||||
'stacktrace' => $this->formatStackTrace($trace),
|
||||
]);
|
||||
echo '</pre>';
|
||||
die();
|
||||
}
|
||||
|
||||
echo 'An <del>un</del>expected error occurred, a team of untrained monkeys has been dispatched to deal with it.';
|
||||
die();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $stackTrace
|
||||
* @return array
|
||||
*/
|
||||
protected function formatStackTrace($stackTrace)
|
||||
{
|
||||
$return = [];
|
||||
|
||||
foreach ($stackTrace as $trace) {
|
||||
$path = '';
|
||||
$line = '';
|
||||
|
||||
if (isset($trace['file']) && isset($trace['line'])) {
|
||||
$path = $this->stripBasePath($trace['file']);
|
||||
$line = $trace['line'];
|
||||
}
|
||||
|
||||
$functionName = $trace['function'];
|
||||
|
||||
$return[] = [
|
||||
'file' => $path . ':' . $line,
|
||||
$functionName => $trace['args'],
|
||||
];
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
protected function stripBasePath($path)
|
||||
{
|
||||
$basePath = realpath(__DIR__ . '/../..') . '/';
|
||||
return str_replace($basePath, '', $path);
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Exceptions\Handlers;
|
||||
|
||||
use Engelsystem\Http\Request;
|
||||
use Throwable;
|
||||
|
||||
interface HandlerInterface
|
||||
{
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Throwable $e
|
||||
*/
|
||||
public function render($request, Throwable $e);
|
||||
|
||||
/**
|
||||
* @param Throwable $e
|
||||
* @return
|
||||
*/
|
||||
public function report(Throwable $e);
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Exceptions\Handlers;
|
||||
|
||||
use Engelsystem\Http\Request;
|
||||
use Throwable;
|
||||
|
||||
class Legacy implements HandlerInterface
|
||||
{
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Throwable $e
|
||||
*/
|
||||
public function render($request, Throwable $e)
|
||||
{
|
||||
echo 'An <del>un</del>expected error occurred, a team of untrained monkeys has been dispatched to deal with it.';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Throwable $e
|
||||
*/
|
||||
public function report(Throwable $e)
|
||||
{
|
||||
error_log(sprintf('Exception: Code: %s, Message: %s, File: %s:%u, Trace: %s',
|
||||
$e->getCode(),
|
||||
$e->getMessage(),
|
||||
$this->stripBasePath($e->getFile()),
|
||||
$e->getLine(),
|
||||
json_encode($e->getTrace())
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
protected function stripBasePath($path)
|
||||
{
|
||||
$basePath = realpath(__DIR__ . '/../../..') . '/';
|
||||
return str_replace($basePath, '', $path);
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Exceptions\Handlers;
|
||||
|
||||
use Engelsystem\Http\Request;
|
||||
use Throwable;
|
||||
|
||||
class LegacyDevelopment extends Legacy
|
||||
{
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Throwable $e
|
||||
*/
|
||||
public function render($request, Throwable $e)
|
||||
{
|
||||
$file = $this->stripBasePath($e->getFile());
|
||||
|
||||
echo '<pre style="background-color:#333;color:#ccc;z-index:1000;position:fixed;bottom:1em;padding:1em;width:97%;max-height: 90%;overflow-y:auto;">';
|
||||
echo sprintf('%s: (%s)' . PHP_EOL, get_class($e), $e->getCode());
|
||||
$data = [
|
||||
'string' => $e->getMessage(),
|
||||
'file' => $file . ':' . $e->getLine(),
|
||||
'stacktrace' => $this->formatStackTrace($e->getTrace()),
|
||||
];
|
||||
var_dump($data);
|
||||
echo '</pre>';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $stackTrace
|
||||
* @return array
|
||||
*/
|
||||
protected function formatStackTrace($stackTrace)
|
||||
{
|
||||
$return = [];
|
||||
$stackTrace = array_reverse($stackTrace);
|
||||
|
||||
foreach ($stackTrace as $trace) {
|
||||
$path = '';
|
||||
$line = '';
|
||||
|
||||
if (isset($trace['file']) && isset($trace['line'])) {
|
||||
$path = $this->stripBasePath($trace['file']);
|
||||
$line = $trace['line'];
|
||||
}
|
||||
|
||||
$functionName = $trace['function'];
|
||||
|
||||
$return[] = [
|
||||
'file' => $path . ':' . $line,
|
||||
$functionName => isset($trace['args']) ? $trace['args'] : null,
|
||||
];
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Exceptions\Handlers;
|
||||
|
||||
use Engelsystem\Application;
|
||||
use Engelsystem\Container\Container;
|
||||
use Engelsystem\Http\Request;
|
||||
use Throwable;
|
||||
use Whoops\Handler\JsonResponseHandler;
|
||||
use Whoops\Handler\PrettyPageHandler;
|
||||
use Whoops\Run as WhoopsRunner;
|
||||
|
||||
class Whoops extends Legacy implements HandlerInterface
|
||||
{
|
||||
/** @var Application */
|
||||
protected $app;
|
||||
|
||||
public function __construct(Container $app)
|
||||
{
|
||||
$this->app = $app;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Throwable $e
|
||||
*/
|
||||
public function render($request, Throwable $e)
|
||||
{
|
||||
$whoops = $this->app->make(WhoopsRunner::class);
|
||||
$handler = $this->getPrettyPageHandler($e);
|
||||
$whoops->pushHandler($handler);
|
||||
|
||||
if ($request->isXmlHttpRequest()) {
|
||||
$handler = $this->getJsonResponseHandler();
|
||||
$whoops->pushHandler($handler);
|
||||
}
|
||||
|
||||
echo $whoops->handleException($e);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Throwable $e
|
||||
* @return PrettyPageHandler
|
||||
*/
|
||||
protected function getPrettyPageHandler(Throwable $e)
|
||||
{
|
||||
$handler = $this->app->make(PrettyPageHandler::class);
|
||||
|
||||
$handler->setPageTitle('Just another ' . get_class($e) . ' to fix :(');
|
||||
$handler->setApplicationPaths([realpath(__DIR__ . '/../..')]);
|
||||
|
||||
$data = $this->getData();
|
||||
$handler->addDataTable('Application', $data);
|
||||
|
||||
return $handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return JsonResponseHandler
|
||||
*/
|
||||
protected function getJsonResponseHandler()
|
||||
{
|
||||
$handler = $this->app->make(JsonResponseHandler::class);
|
||||
$handler->setJsonApi(true);
|
||||
$handler->addTraceToOutput(true);
|
||||
|
||||
return $handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Aggregate application data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getData()
|
||||
{
|
||||
global $user;
|
||||
|
||||
$data = [];
|
||||
$data['user'] = $user;
|
||||
$data['Booted'] = $this->app->isBooted();
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Exceptions\handlers;
|
||||
|
||||
|
||||
use Engelsystem\Exceptions\Handlers\LegacyDevelopment;
|
||||
use Engelsystem\Http\Request;
|
||||
use ErrorException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PHPUnit_Framework_MockObject_MockObject as Mock;
|
||||
|
||||
class LegacyDevelopmentTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Exceptions\Handlers\LegacyDevelopment::render()
|
||||
* @covers \Engelsystem\Exceptions\Handlers\LegacyDevelopment::formatStackTrace()
|
||||
*/
|
||||
public function testRender()
|
||||
{
|
||||
$handler = new LegacyDevelopment();
|
||||
/** @var Request|Mock $request */
|
||||
$request = $this->createMock(Request::class);
|
||||
$exception = new ErrorException('Lorem Ipsum', 4242, 1, 'foo.php', 9999);
|
||||
|
||||
$regex = sprintf(
|
||||
'%%<pre.*>.*ErrorException.*4242.*Lorem Ipsum.*%s.*%s.*%s.*</pre>%%is',
|
||||
'foo.php',
|
||||
9999,
|
||||
__FUNCTION__
|
||||
);
|
||||
$this->expectOutputRegex($regex);
|
||||
|
||||
$handler->render($request, $exception);
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Exceptions\handlers;
|
||||
|
||||
|
||||
use Engelsystem\Exceptions\Handlers\Legacy;
|
||||
use Engelsystem\Http\Request;
|
||||
use Exception;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PHPUnit_Framework_MockObject_MockObject as Mock;
|
||||
|
||||
class LegacyTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Exceptions\Handlers\Legacy::render()
|
||||
*/
|
||||
public function testRender()
|
||||
{
|
||||
$handler = new Legacy();
|
||||
/** @var Request|Mock $request */
|
||||
$request = $this->createMock(Request::class);
|
||||
/** @var Exception|Mock $exception */
|
||||
$exception = $this->createMock(Exception::class);
|
||||
|
||||
$this->expectOutputRegex('/.*error occurred.*/i');
|
||||
|
||||
$handler->render($request, $exception);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Exceptions\Handlers\Legacy::report()
|
||||
* @covers \Engelsystem\Exceptions\Handlers\Legacy::stripBasePath()
|
||||
*/
|
||||
public function testReport()
|
||||
{
|
||||
$handler = new Legacy();
|
||||
$exception = new Exception('Lorem Ipsum', 4242);
|
||||
$line = __LINE__ - 1;
|
||||
|
||||
$log = tempnam(sys_get_temp_dir(), 'engelsystem-log');
|
||||
$errorLog = ini_get('error_log');
|
||||
ini_set('error_log', $log);
|
||||
$handler->report($exception);
|
||||
ini_set('error_log', $errorLog);
|
||||
$logContent = file_get_contents($log);
|
||||
unset($log);
|
||||
|
||||
$this->assertContains('4242', $logContent);
|
||||
$this->assertContains('Lorem Ipsum', $logContent);
|
||||
$this->assertContains(basename(__FILE__), $logContent);
|
||||
$this->assertContains((string)$line, $logContent);
|
||||
$this->assertContains(__FUNCTION__, $logContent);
|
||||
$this->assertContains(json_encode(__CLASS__), $logContent);
|
||||
}
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Exceptions\handlers;
|
||||
|
||||
|
||||
use Engelsystem\Application;
|
||||
use Engelsystem\Exceptions\Handlers\Whoops;
|
||||
use Engelsystem\Http\Request;
|
||||
use Exception;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PHPUnit_Framework_MockObject_MockObject as Mock;
|
||||
use Whoops\Handler\JsonResponseHandler;
|
||||
use Whoops\Handler\PrettyPageHandler;
|
||||
use Whoops\Run as WhoopsRunner;
|
||||
use Whoops\RunInterface as WhoopsRunnerInterface;
|
||||
|
||||
class WhoopsTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Exceptions\Handlers\Whoops
|
||||
*/
|
||||
public function testRender()
|
||||
{
|
||||
/** @var Application|Mock $app */
|
||||
$app = $this->createMock(Application::class);
|
||||
/** @var Request|Mock $request */
|
||||
$request = $this->createMock(Request::class);
|
||||
$request->expects($this->once())
|
||||
->method('isXmlHttpRequest')
|
||||
->willReturn(true);
|
||||
/** @var WhoopsRunnerInterface|Mock $whoopsRunner */
|
||||
$whoopsRunner = $this->getMockForAbstractClass(WhoopsRunnerInterface::class);
|
||||
/** @var PrettyPageHandler|Mock $prettyPageHandler */
|
||||
$prettyPageHandler = $this->createMock(PrettyPageHandler::class);
|
||||
$prettyPageHandler
|
||||
->expects($this->atLeastOnce())
|
||||
->method('setApplicationPaths');
|
||||
$prettyPageHandler
|
||||
->expects($this->once())
|
||||
->method('setApplicationPaths');
|
||||
$prettyPageHandler
|
||||
->expects($this->once())
|
||||
->method('addDataTable');
|
||||
/** @var JsonResponseHandler|Mock $jsonResponseHandler */
|
||||
$jsonResponseHandler = $this->createMock(JsonResponseHandler::class);
|
||||
$jsonResponseHandler->expects($this->once())
|
||||
->method('setJsonApi')
|
||||
->with(true);
|
||||
$jsonResponseHandler->expects($this->once())
|
||||
->method('addTraceToOutput')
|
||||
->with(true);
|
||||
/** @var Exception|Mock $exception */
|
||||
$exception = $this->createMock(Exception::class);
|
||||
|
||||
$app->expects($this->exactly(3))
|
||||
->method('make')
|
||||
->withConsecutive(
|
||||
[WhoopsRunner::class],
|
||||
[PrettyPageHandler::class],
|
||||
[JsonResponseHandler::class]
|
||||
)
|
||||
->willReturnOnConsecutiveCalls(
|
||||
$whoopsRunner,
|
||||
$prettyPageHandler,
|
||||
$jsonResponseHandler
|
||||
);
|
||||
|
||||
$whoopsRunner
|
||||
->expects($this->exactly(2))
|
||||
->method('pushHandler')
|
||||
->withConsecutive(
|
||||
[$prettyPageHandler],
|
||||
[$jsonResponseHandler]
|
||||
);
|
||||
$whoopsRunner
|
||||
->expects($this->once())
|
||||
->method('handleException')
|
||||
->with($exception);
|
||||
|
||||
$handler = new Whoops($app);
|
||||
$handler->render($request, $exception);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue