87 lines
3.3 KiB
PHP
87 lines
3.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* This file is part of the TYPO3 CMS project.
|
|
*
|
|
* It is free software; you can redistribute it and/or modify it under
|
|
* the terms of the GNU General Public License, either version 2
|
|
* of the License, or any later version.
|
|
*
|
|
* For the full copyright and license information, please read the
|
|
* LICENSE.txt file that was distributed with this source code.
|
|
*
|
|
* The TYPO3 project - inspiring people to share!
|
|
*/
|
|
|
|
namespace TYPO3\CMS\Frontend\ContentObject\Exception;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
|
|
use TYPO3\CMS\Core\Context\Context;
|
|
use TYPO3\CMS\Core\Core\RequestId;
|
|
use TYPO3\CMS\Core\Crypto\Random;
|
|
use TYPO3\CMS\Core\Error\AbstractExceptionHandler;
|
|
use TYPO3\CMS\Core\Http\ImmediateResponseException;
|
|
use TYPO3\CMS\Frontend\ContentObject\AbstractContentObject;
|
|
|
|
/**
|
|
* Exception handler class for content object rendering
|
|
* @internal this is a concrete TYPO3 implementation and solely used for EXT:frontend and not part of TYPO3's Core API.
|
|
*/
|
|
#[Autoconfigure(public: true, shared: false)]
|
|
class ProductionExceptionHandler implements ExceptionHandlerInterface
|
|
{
|
|
protected array $configuration = [];
|
|
|
|
public function __construct(
|
|
protected Context $context,
|
|
protected Random $random,
|
|
protected LoggerInterface $logger,
|
|
protected RequestId $requestId
|
|
) {}
|
|
|
|
public function setConfiguration(array $configuration): void
|
|
{
|
|
$this->configuration = $configuration;
|
|
}
|
|
|
|
/**
|
|
* Handles exceptions thrown during rendering of content objects
|
|
* The handler can decide whether to re-throw the exception or
|
|
* return a nice error message for production context.
|
|
*
|
|
* @param array $contentObjectConfiguration
|
|
* @throws \Exception
|
|
*/
|
|
public function handle(\Exception $exception, ?AbstractContentObject $contentObject = null, $contentObjectConfiguration = []): string
|
|
{
|
|
// ImmediateResponseException (and the derived PropagateResponseException) should work similar to
|
|
// exit / die and must therefore not be handled by this ExceptionHandler.
|
|
if ($exception instanceof ImmediateResponseException) {
|
|
throw $exception;
|
|
}
|
|
|
|
if (!empty($this->configuration['ignoreCodes.'])
|
|
&& in_array($exception->getCode(), array_map('intval', $this->configuration['ignoreCodes.']), true)
|
|
) {
|
|
throw $exception;
|
|
}
|
|
|
|
$errorMessage = $this->configuration['errorMessage'] ?? 'Oops, an error occurred! Request: {requestId}';
|
|
|
|
// $code and it's placeholder %s for b/w compatibility
|
|
$code = $this->context->getAspect('date')->getDateTime()->format('YmdHis') . $this->random->generateRandomHexString(8);
|
|
$errorMessage = str_replace('%s', '{code}', $errorMessage);
|
|
|
|
// Log exception except HMAC validation exceptions caused by potentially forged requests
|
|
if (!in_array($exception->getCode(), AbstractExceptionHandler::IGNORED_HMAC_EXCEPTION_CODES, true)) {
|
|
$this->logger->alert($errorMessage, ['exception' => $exception, 'code' => $code, 'requestId' => $this->requestId]);
|
|
}
|
|
|
|
// Return interpolated error message
|
|
return str_replace(['{code}', '{requestId}'], [$code, (string)$this->requestId], $errorMessage);
|
|
}
|
|
}
|