catch all possible exceptions and errors

This commit is contained in:
Daniel Seifert 2022-08-16 11:08:05 +02:00
parent 88b6a1d6fa
commit 922d26d3ac
Signed by: DanielS
GPG Key ID: 8A7C4C6ED1915C6F
4 changed files with 263 additions and 137 deletions

View File

@ -0,0 +1,90 @@
<?php
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
*/
declare(strict_types=1);
namespace D3\DebugBar\Application\Models;
use D3\DebugBar\Application\Component\DebugBarComponent;
use D3\DebugBar\Core\DebugBarErrorHandler;
use D3\DebugBar\Core\DebugBarExceptionHandler;
use OxidEsales\Eshop\Core\Registry;
class DebugBarHandler
{
/**
* @return void
*/
public function setErrorHandler(): void
{
if ($this->d3CanActivateDebugBar()) {
set_error_handler(
[
new DebugBarErrorHandler(),
'callback'
],
$this->getHandledErrorTypes()
);
}
}
/**
* @return int
*/
protected function getHandledErrorTypes(): int
{
return E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_PARSE;
}
/**
* @return void
*/
public function setExceptionHandler(): void
{
if ($this->d3CanActivateDebugBar()) {
set_exception_handler( [
new DebugBarExceptionHandler(),
'handleUncaughtException'
] );
}
}
/**
* @return void
*/
public function addDebugBarComponent(): void
{
if ($this->d3CanActivateDebugBar()) {
$userComponentNames = Registry::getConfig()->getConfigParam( 'aUserComponentNames' );
$d3CmpName = DebugBarComponent::class;
$blDontUseCache = 1;
if ( ! is_array( $userComponentNames ) ) {
$userComponentNames = [];
}
if ( ! in_array( $d3CmpName, array_keys( $userComponentNames ) ) ) {
$userComponentNames[ $d3CmpName ] = $blDontUseCache;
Registry::getConfig()->setConfigParam( 'aUserComponentNames', $userComponentNames );
}
}
}
/**
* @return bool
*/
protected function d3CanActivateDebugBar(): bool
{
return false === isAdmin();
}
}

View File

@ -0,0 +1,99 @@
<?php
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
*/
declare(strict_types=1);
namespace D3\DebugBar\Core;
use D3\DebugBar\Application\Models\Exceptions\CompileErrorException;
use D3\DebugBar\Application\Models\Exceptions\CoreErrorException;
use D3\DebugBar\Application\Models\Exceptions\ParseException;
use D3\DebugBar\Application\Models\Exceptions\UserErrorException;
use ErrorException;
use OxidEsales\Eshop\Core\Registry;
class DebugBarErrorHandler
{
/**
* @param $severity
* @param $message
* @param $file
* @param $line
*
* @return void|false
* @throws CompileErrorException
* @throws CoreErrorException
* @throws ErrorException
* @throws ParseException
* @throws UserErrorException
*/
public function callback( $severity, $message, $file, $line )
{
global $debugBarErrorOccured;
$debugBarErrorOccured = 1;
if ( 0 === error_reporting() || !( error_reporting() & $severity ) ) {
// This error code is not included in error_reporting.
return false;
}
$smartyTemplate = $this->getSmartyTemplateLocationFromError( $message );
if ( is_array( $smartyTemplate ) ) {
[ $file, $line ] = $smartyTemplate;
}
switch($severity) {
case E_CORE_ERROR:
throw new CoreErrorException($message, 0, $severity, $file, $line);
case E_COMPILE_ERROR:
throw new CompileErrorException($message, 0, $severity, $file, $line);
case E_USER_ERROR:
throw new UserErrorException($message, 0, $severity, $file, $line);
case E_PARSE:
throw new ParseException($message, 0, $severity, $file, $line);
case E_ERROR:
throw new ErrorException($message, 0, $severity, $file, $line);
default:
$this->handleUnregisteredErrorTypes($message, $severity, $file, $line);
}
}
/**
* @param $messsage
* @return array|null
*/
protected function getSmartyTemplateLocationFromError($messsage): ?array
{
if (stristr($messsage, 'Smarty error: [in ')) {
$start = strpos($messsage, '[')+1;
$end = strpos($messsage, ']');
$parts = explode(' ', substr($messsage, $start, $end - $start));
return [Registry::getConfig()->getTemplateDir(isAdmin()).$parts[1], (int) $parts[3]];
}
return null;
}
/**
* @param string $message
* @param int|null $severity
* @param string|null $file
* @param int|null $line
*
* @throws ErrorException
*/
protected function handleUnregisteredErrorTypes(string $message = '', int $severity = null, string $file = null, int $line = null)
{
throw new ErrorException($message, 0, $severity, $file, $line);
}
}

View File

@ -18,8 +18,7 @@ namespace D3\DebugBar\Core;
use D3\DebugBar\Application\Component\DebugBarComponent; use D3\DebugBar\Application\Component\DebugBarComponent;
use DebugBar\DataCollector\ExceptionsCollector; use DebugBar\DataCollector\ExceptionsCollector;
use DebugBar\DebugBarException; use DebugBar\DebugBarException;
use OxidEsales\Eshop\Application\Controller\FrontendController; use OxidEsales\Eshop\Core\ConfigFile;
use OxidEsales\Eshop\Core\Exception\DatabaseException;
use OxidEsales\Eshop\Core\Exception\ExceptionHandler; use OxidEsales\Eshop\Core\Exception\ExceptionHandler;
use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Registry;
use OxidEsales\EshopCommunity\Internal\Framework\Logger\LoggerServiceFactory; use OxidEsales\EshopCommunity\Internal\Framework\Logger\LoggerServiceFactory;
@ -32,12 +31,11 @@ class DebugBarExceptionHandler
* Handler for uncaught exceptions. * Handler for uncaught exceptions.
* *
* @param Throwable $exception exception object * @param Throwable $exception exception object
* @throws DebugBarException
*/ */
public function handleUncaughtException(Throwable $exception) public function handleUncaughtException(Throwable $exception)
{ {
try { try {
$debugMode = (bool) \OxidEsales\Eshop\Core\Registry::get(\OxidEsales\Eshop\Core\ConfigFile::class)->getVar('iDebug'); $debugMode = (bool) Registry::get( ConfigFile::class)->getVar( 'iDebug');
$defaultExceptionHandler = new ExceptionHandler($debugMode); $defaultExceptionHandler = new ExceptionHandler($debugMode);
$defaultExceptionHandler->writeExceptionToLog($exception); $defaultExceptionHandler->writeExceptionToLog($exception);
} catch (Throwable $loggerException) { } catch (Throwable $loggerException) {
@ -46,7 +44,7 @@ class DebugBarExceptionHandler
* Try again to log original exception (without DI container) in order to show the root cause of a problem. * Try again to log original exception (without DI container) in order to show the root cause of a problem.
*/ */
try { try {
$loggerServiceFactory = new LoggerServiceFactory(new Context(Registry::getConfig())); $loggerServiceFactory = new LoggerServiceFactory(new Context());
$logger = $loggerServiceFactory->getLogger(); $logger = $loggerServiceFactory->getLogger();
$logger->error($exception->getTraceAsString()); $logger->error($exception->getTraceAsString());
} catch (Throwable $throwableWithoutPossibilityToWriteToLogFile) { } catch (Throwable $throwableWithoutPossibilityToWriteToLogFile) {
@ -55,7 +53,9 @@ class DebugBarExceptionHandler
} }
global $debugBarSet; global $debugBarSet;
if ($debugBarSet !== 1 && false === isAdmin()) { if ($debugBarSet !== 1 && false === isAdmin()) {
try {
/** @var DebugBarComponent $debugBarComponent */ /** @var DebugBarComponent $debugBarComponent */
$debugBarComponent = oxNew( DebugBarComponent::class ); $debugBarComponent = oxNew( DebugBarComponent::class );
@ -65,8 +65,9 @@ class DebugBarExceptionHandler
echo <<<HTML echo <<<HTML
<!DOCTYPE html> <!DOCTYPE html>
<html> <html lang="en">
<head> <head>
<title></title>
HTML; HTML;
echo $debugBarComponent->getRenderer()->renderHead(); echo $debugBarComponent->getRenderer()->renderHead();
$debugBarComponent->addTimelineMessures(); $debugBarComponent->addTimelineMessures();
@ -80,16 +81,10 @@ HTML;
</body> </body>
</html> </html>
HTML; HTML;
} catch (DebugBarException $e) {
Registry::getLogger()->error($e);
Registry::getUtilsView()->addErrorToDisplay($e);
} }
} }
/**
* @param DatabaseException $exception
* @return void
* @throws DebugBarException
*/
public function handleDatabaseException(DatabaseException $exception)
{
$this->handleUncaughtException($exception);
} }
} }

View File

@ -16,138 +16,80 @@ declare(strict_types=1);
namespace D3\DebugBar\Modules\Core; namespace D3\DebugBar\Modules\Core;
use D3\DebugBar\Application\Component\DebugBarComponent; use D3\DebugBar\Application\Component\DebugBarComponent;
use D3\DebugBar\Application\Models\Exceptions\CompileErrorException; use D3\DebugBar\Application\Models\DebugBarHandler;
use D3\DebugBar\Application\Models\Exceptions\CoreErrorException;
use D3\DebugBar\Application\Models\Exceptions\ParseException;
use D3\DebugBar\Application\Models\Exceptions\UserErrorException;
use D3\DebugBar\Core\DebugBarExceptionHandler; use D3\DebugBar\Core\DebugBarExceptionHandler;
use DebugBar\DataCollector\ExceptionsCollector; use OxidEsales\Eshop\Core\Exception\StandardException;
use ErrorException;
use OxidEsales\Eshop\Application\Controller\FrontendController;
use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Registry;
use Throwable;
class ShopControl_DebugBar extends ShopControl_DebugBar_parent class ShopControl_DebugBar extends ShopControl_DebugBar_parent
{ {
/**
* @throws ErrorException
*/
public function __construct() public function __construct()
{ {
$this->d3DebugBarSetErrorHandler();; $handler = oxNew(DebugBarHandler::class);
$this->d3DebugBarSetExceptionHandler();
$this->d3AddDebugBarComponent(); $handler->setErrorHandler();
$handler->setExceptionHandler();
$handler->addDebugBarComponent();
parent::__construct(); parent::__construct();
} }
/** /**
* @return void * @param string|null $controllerKey
* @throws ErrorException * @param string|null $function
* @param string|null $parameters
* @param string|null $viewsChain
*/ */
public function d3DebugBarSetErrorHandler() public function start($controllerKey = null, $function = null, $parameters = null, $viewsChain = null)
{ {
if ($this->d3CanActivateDebugBar()) { parent::start();
set_error_handler(
function( $severity, $message, $file, $line ) {
if ( 0 === error_reporting() || !( error_reporting() & $severity ) ) {
// This error code is not included in error_reporting.
return false;
}
$smartyTemplate = $this->getSmartyTemplateLocationFromError( $message ); global $debugBarSet, $debugBarErrorOccured;
if ( is_array( $smartyTemplate ) ) {
[ $file, $line ] = $smartyTemplate;
}
switch($severity) { if (!isAdmin() && $debugBarSet !== 1 && $debugBarErrorOccured !== 1) {
case E_CORE_ERROR:
throw new CoreErrorException($message, 0, $severity, $file, $line);
case E_COMPILE_ERROR:
throw new CompileErrorException($message, 0, $severity, $file, $line);
case E_USER_ERROR:
throw new UserErrorException($message, 0, $severity, $file, $line);
case E_PARSE:
throw new ParseException($message, 0, $severity, $file, $line);
case E_ERROR:
default:
throw new ErrorException($message, 0, $severity, $file, $line);
}
},
E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_PARSE
);
}
}
/**
* @return void
*/
protected function d3DebugBarSetExceptionHandler(): void
{
if ($this->d3CanActivateDebugBar()) {
set_exception_handler( [
new DebugBarExceptionHandler(),
'handleUncaughtException'
] );
}
}
/**
* @param $messsage
* @return array|null
*/
protected function getSmartyTemplateLocationFromError($messsage)
{
if (stristr($messsage, 'Smarty error: [in ')) {
$start = strpos($messsage, '[')+1;
$end = strpos($messsage, ']');
$parts = explode(' ', substr($messsage, $start, $end - $start));
return [Registry::getConfig()->getTemplateDir(isAdmin()).$parts[1], (int) $parts[3]];
}
return null;
}
/**
* @return void
*/
protected function d3AddDebugBarComponent(): void
{
if ($this->d3CanActivateDebugBar()) {
$userComponentNames = Registry::getConfig()->getConfigParam( 'aUserComponentNames' );
$d3CmpName = DebugBarComponent::class;
$blDontUseCache = 1;
if ( ! is_array( $userComponentNames ) ) {
$userComponentNames = [];
}
if ( ! in_array( $d3CmpName, array_keys( $userComponentNames ) ) ) {
$userComponentNames[ $d3CmpName ] = $blDontUseCache;
Registry::getConfig()->setConfigParam( 'aUserComponentNames', $userComponentNames );
}
}
}
/**
* @return bool
*/
protected function d3CanActivateDebugBar(): bool
{
return false === isAdmin();
}
public function __destruct()
{
global $debugBarSet;
if (!isAdmin() && $debugBarSet !== 1) {
$activeView = Registry::getConfig()->getTopActiveView(); $activeView = Registry::getConfig()->getTopActiveView();
/** @var DebugBarComponent|null $debugBarComponent */ /** @var DebugBarComponent|null $debugBarComponent */
$debugBarComponent = $activeView->getComponent(DebugBarComponent::class); $debugBarComponent = $activeView->getComponent(DebugBarComponent::class);
if ($debugBarComponent) { if ($debugBarComponent) {
$debugBarSet = 1;
echo $debugBarComponent->getRenderer()->renderHead(); echo $debugBarComponent->getRenderer()->renderHead();
$debugBarComponent->addTimelineMessures(); $debugBarComponent->addTimelineMessures();
echo $debugBarComponent->getRenderer()->render(); echo $debugBarComponent->getRenderer()->render();
} }
} }
} }
/**
* @param Throwable $exception
*/
protected function debugBarHandleException(Throwable $exception)
{
$exceptionHandler = new DebugBarExceptionHandler();
$exceptionHandler->handleUncaughtException($exception);
}
/**
* @param StandardException $exception
*/
protected function _handleSystemException($exception)
{
$this->debugBarHandleException($exception);
}
/**
* @param StandardException $exception
*/
protected function _handleCookieException($exception)
{
$this->debugBarHandleException($exception);
}
/**
* @param StandardException $exception
*/
protected function _handleBaseException($exception)
{
$this->debugBarHandleException($exception);
}
} }