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

问题描述 投票:3回答:2

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

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

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

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

预期结果

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

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

我尝试了什么

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

throw new \Exception('Invalid data');

但是,所有前端显示都是

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

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

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

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

php exception typo3 typo3-7.6.x
2个回答
1
投票

你含蓄地问了两个问题:

1. How do I correctly throw an exception in my code?

我认为,这是正确的,你做了什么:只使用PHP \ Exception或从\ Exception继承的合适的异常:

throw new \UnexpectedValueException('Invalid data');

2. Once an exception has been thrown, how do I see more information?

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

在开发系统上:

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

Error and ExceptionHandling Example

在生产系统上:

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

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

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/

© www.soinside.com 2019 - 2024. All rights reserved.