Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
TASK: add ProductionExceptionHandler to catch all Exceptions and retu…
Browse files Browse the repository at this point in the history
…rn exception codes to the frontend in AJAX
  • Loading branch information
kabarakh committed Feb 12, 2018
1 parent 8653d69 commit b2b709f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 43 deletions.
57 changes: 14 additions & 43 deletions Classes/Controller/AbstractApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public function injectLogger(\PunktDe\PtExtbase\Logger\Logger $logger)
}



/**
* Handles all exceptions thrown inside the application
*/
Expand All @@ -56,30 +55,15 @@ protected function callActionMethod()
try {
parent::callActionMethod();
} catch (Exception $exception) {
if ($exception instanceof \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException) {
throw $exception;
}

header('HTTP/1.1 500 Internal Server Error', true, 500);

$this->response->setContent($exception->getCode());

if ($exception instanceof \PunktDe\PtExtbase\Exception\LoggerException) {
$this->logger->log($exception->getLogLevel(), sprintf('Code %s', $exception->getCode()), get_class($this), ['exception' => $exception->getMessage()]);
} else {
$this->logger->error(sprintf('Code %s', $exception->getCode()), get_class($this), ['exception' => $exception->getMessage()]);
if (!($exception instanceof \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException)) {
$this->cleanUpAtException($exception);
}

$this->objectManager->get('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager')->persistAll();

$this->cleanUpAtException($exception);

return $exception->getCode();
throw $exception;
}
}



/**
* Use this template method in own ApiController to implement further steps if an exception is thrown
*
Expand All @@ -90,25 +74,26 @@ protected function cleanUpAtException(Exception $exception)
}



/**
* Handles all errors thrown during the dispatcher / validation phase
*
* @return void
*/
protected function errorAction()
{
header('HTTP/1.1 500 Internal Server Error', true, 500);
$error = $this->findFirstError($this->arguments->getValidationResults());

$this->handleError($this->findFirstError($this->arguments->getValidationResults()));
if ($error instanceof Error) {
$error = new Error('Unknown Error while dispatching the controller action.', 1400683671);
}

$this->objectManager->get('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager')->persistAll();
$this->logger->error(sprintf('%s (%s)', $error->getMessage(), $error->getCode()), get_class($this));

die();
throw new \TYPO3\CMS\Core\Error\Http\StatusException([\TYPO3\CMS\Core\Utility\HttpUtility::HTTP_STATUS_500],
$error->getMessage(), '', $error->getCode());
}



/**
* @param \TYPO3\CMS\Extbase\Error\Result $validationResult
* @return Error
Expand All @@ -120,28 +105,14 @@ protected function findFirstError(\TYPO3\CMS\Extbase\Error\Result $validationRes
return $error;
}

foreach ($validationResult->getSubResults() as $argumentName => $subValidationResult) { /** @var $subValidationResult \TYPO3\CMS\Extbase\Error\Result */
foreach ($validationResult->getSubResults() as $argumentName => $subValidationResult) {
/** @var $subValidationResult \TYPO3\CMS\Extbase\Error\Result */
$error = $this->findFirstError($subValidationResult);
if ($error instanceof Error) {
return $error;
}
}
}



/**
* @param mixed $error
* @return void
*/
protected function handleError($error)
{
if ($error instanceof Error) {
$this->logger->error(sprintf('%s (%s)', $error->getMessage(), $error->getCode()));
echo $error->getCode();
} else {
$this->logger->error('Unknown Error while dispatching the controller action. (1400683671)', get_class($this));
echo 1400683671;
}
return null;
}
}
}
62 changes: 62 additions & 0 deletions Classes/ExceptionHandler/ProductionExceptionHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
namespace PunktDe\PtExtbase\ExceptionHandler;

/*
* This file is part of the PunktDe\PtExtbase package.
*
* This package is open source software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use TYPO3\CMS\Core\Error\ProductionExceptionHandler as Typo3ProductionExceptionHandler;
use TYPO3\CMS\Core\Messaging\AbstractStandaloneMessage;
use TYPO3\CMS\Core\Messaging\ErrorpageMessage;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class ProductionExceptionHandler extends Typo3ProductionExceptionHandler
{

/**
* @param \Exception|\Throwable $exception
*/
public function echoExceptionWeb($exception)
{
if (isset($GLOBALS['TYPO3_AJAX']) && $GLOBALS['TYPO3_AJAX'] === true) {
echo $exception->getCode();
} else {
$this->sendStatusHeaders($exception);

$this->writeLogEntries($exception, self::CONTEXT_WEB);

$messageObj = GeneralUtility::makeInstance(
ErrorpageMessage::class,
$this->getMessage($exception),
$this->getTitle($exception)
);
$messageObj = $this->overrideDisplayMessageForWeb($messageObj);

$messageObj->output();
}
}

/**
* This method can be overwritten to control the message the users will be seeing.
*
* E.g. you can just set another html file by calling $errorpageMessage->setTemplateFile() or you could
* create your own object extending the TYPO3 AbstractStandaloneMessage and do your own output
*
* If you want to use this or your own ProductionExceptionHandler, make sure to configure them in the install tool/
* LocalConfiguration by setting
* $TYPO3_CONF_VARS['SYS']['productionExceptionHandler'] = \PunktDe\PtExtbase\ExceptionHandler\ProductionExceptionHandler::class
* (or similar)
*
* @param ErrorpageMessage $errorpageMessage
* @return AbstractStandaloneMessage
*/
protected function overrideDisplayMessageForWeb(ErrorpageMessage $errorpageMessage): AbstractStandaloneMessage
{
return $errorpageMessage;
}

}
2 changes: 2 additions & 0 deletions Classes/Utility/eIDDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
die('Access denied.');
}

$GLOBALS['TYPO3_AJAX'] = true;

require_once \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('pt_extbase') . 'Classes/Utility/AjaxDispatcher.php';


Expand Down

0 comments on commit b2b709f

Please sign in to comment.