如何在你的扩展中抛出异常?

时间:2016-07-21 07:36:17

标签: php exception typo3 typo3-7.6.x

在Extbase扩展中,可能需要通知用户错误或异常。

在我的情况下,我必须从一个可能不好的来源解析一些数据。因此扩展必须验证此数据。如果数据无效,则需要抛出一个异常,然后由TYPO3处理。

但是,我只能找到有关异常和错误处理程序如何工作的信息,但是没有关于如何从扩展内部正确抛出异常的信息。

那么从Extbase扩展内部抛出异常的目的是什么?

预期结果

如果我产生语法错误,TYPO3会显示类似于以下内容的消息: enter image description here (取自the core API reference。)

这就是我所期望的正确抛出错误或异常的样子。

我尝试了什么

编辑:我尝试抛出这样的错误:

throw new \Exception('Invalid data');

但是,所有前端显示都是

  

糟糕,发生错误!代码:20160721101726b5339896

产生错误的另一种可能方法:

$GLOBALS['TSFE']->pageNotFoundAndExit('Invalid data');

但是,这会显示“找不到页面”错误而不是预期的异常。

2 个答案:

答案 0 :(得分:1)

namespace VendorName\ExtensionName\Controller;

abstract class ActionController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
    /**
     * @var string
     */
    protected $entityNotFoundMessage = 'The requested entity could not be found.';

    /**
     * @var string
     */
    protected $unknownErrorMessage = 'An unknown error occurred. The wild monkey horde in our basement will try to fix this as soon as possible.';

    /**
     * @param \TYPO3\CMS\Extbase\Mvc\RequestInterface $request
     * @param \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response
     * @return void
     * @throws \Exception
     * @override \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
     */
    public function processRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response) {
        try {
            parent::processRequest($request, $response);
        }
        catch(\Exception $exception) {
            // If the property mapper did throw a \TYPO3\CMS\Extbase\Property\Exception, because it was unable to find the requested entity, call the page-not-found handler.
            $previousException = $exception->getPrevious();
            if (($exception instanceof \TYPO3\CMS\Extbase\Property\Exception) && (($previousException instanceof \TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException) || ($previousException instanceof \TYPO3\CMS\Extbase\Property\Exception\InvalidSourceException))) {
                $GLOBALS['TSFE']->pageNotFoundAndExit($this->entityNotFoundMessage);
            }
            throw $exception;
        }
    }

    /**
     * @return void
     * @override \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
     */
    protected function callActionMethod() {
        try {
            parent::callActionMethod();
        }
        catch(\Exception $exception) {
            // This enables you to trigger the call of TYPO3s page-not-found handler by throwing \TYPO3\CMS\Core\Error\Http\PageNotFoundException
            if ($exception instanceof \TYPO3\CMS\Core\Error\Http\PageNotFoundException) {
                $GLOBALS['TSFE']->pageNotFoundAndExit($this->entityNotFoundMessage);
            }

            // $GLOBALS['TSFE']->pageNotFoundAndExit has not been called, so the exception is of unknown type.
            \VendorName\ExtensionName\Logger\ExceptionLogger::log($exception, $this->request->getControllerExtensionKey(), \VendorName\ExtensionName\Logger\ExceptionLogger::SEVERITY_FATAL_ERROR);
            // If the plugin is configured to do so, we call the page-unavailable handler.
            if (isset($this->settings['usePageUnavailableHandler']) && $this->settings['usePageUnavailableHandler']) {
                $GLOBALS['TSFE']->pageUnavailableAndExit($this->unknownErrorMessage, 'HTTP/1.1 500 Internal Server Error');
            }
            // Else we append the error message to the response. This causes the error message to be displayed inside the normal page layout. WARNING: the plugins output may gets cached.
            if ($this->response instanceof \TYPO3\CMS\Extbase\Mvc\Web\Response) {
                $this->response->setStatus(500);
            }
            $this->response->appendContent($this->unknownErrorMessage);
        }
    }
}

这是一篇解释这一点的文章。 但是大多数关于TYPO3编程的文章都是用德语写的; - )

http://nerdcenter.de/extbase-fehlerbehandlung/

答案 1 :(得分:1)

您暗中问了两个问题:

1。如何在我的代码中正确抛出异常?

我认为这是正确的:您只需使用PHP \ Exception或从\ Exception继承的适当异常即可:

throw new \UnexpectedValueException('Invalid data');

2。引发异常后,如何查看更多信息?

这已经得到很好的回答:https://stackoverflow.com/a/34067853/2444812

在开发系统上:

  • 设置配置预设“调试”
  • 在起始页上添加TypoScript:config.contentObjectExceptionHandler = 0

请参见Error and ExceptionHandling Example

在生产系统上:

您通常不希望在前端看到完整的堆栈跟踪。这就是为什么config.contentObjectExceptionHandler通常设置为默认值(仅显示 Oops,发生错误!)的原因!呈现页面上的代码:20160721101726b5339896 。但是,使用此代码,您可以查看日志:

  • sys_log:请参阅后端中的“日志”
  • 日志文件:typo3temp / var / logs / typo3-error.log(请参阅Logging with TYPO3)。可能是较旧版本的typo3temp / logs。