‪TYPO3CMS  ‪main
ProductionExceptionHandler.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Log\LoggerInterface;
27 
33 {
34  protected array ‪$configuration = [];
35 
36  public function ‪__construct(
37  protected ‪Context $context,
38  protected ‪Random $random,
39  protected LoggerInterface $logger,
40  protected ‪RequestId $requestId
41  ) {}
42 
43  public function ‪setConfiguration(array ‪$configuration): void
44  {
45  $this->configuration = ‪$configuration;
46  }
47 
57  public function ‪handle(\‪Exception $exception, ‪AbstractContentObject $contentObject = null, $contentObjectConfiguration = []): string
58  {
59  // ImmediateResponseException (and the derived PropagateResponseException) should work similar to
60  // exit / die and must therefore not be handled by this ExceptionHandler.
61  if ($exception instanceof ‪ImmediateResponseException) {
62  throw $exception;
63  }
64 
65  if (!empty($this->configuration['ignoreCodes.'])
66  && in_array($exception->getCode(), array_map('intval', $this->configuration['ignoreCodes.']), true)
67  ) {
68  throw $exception;
69  }
70 
71  $errorMessage = $this->configuration['errorMessage'] ?? 'Oops, an error occurred! Request: {requestId}';
72 
73  // $code and it's placeholder %s for b/w compatibility
74  $code = $this->context->getAspect('date')->getDateTime()->format('YmdHis') . $this->random->generateRandomHexString(8);
75  $errorMessage = str_replace('%s', '{code}', $errorMessage);
76 
77  // Log exception except HMAC validation exceptions caused by potentially forged requests
78  if (!in_array($exception->getCode(), ‪AbstractExceptionHandler::IGNORED_HMAC_EXCEPTION_CODES, true)) {
79  $this->logger->alert($errorMessage, ['exception' => $exception, 'code' => $code, 'requestId' => $this->requestId]);
80  }
81 
82  // Return interpolated error message
83  return str_replace(['{code}', '{requestId}'], [$code, $this->requestId], $errorMessage);
84  }
85 }
‪TYPO3\CMS\Frontend\ContentObject\Exception\ProductionExceptionHandler\setConfiguration
‪setConfiguration(array $configuration)
Definition: ProductionExceptionHandler.php:43
‪TYPO3\CMS\Core\Error\AbstractExceptionHandler\IGNORED_HMAC_EXCEPTION_CODES
‪const IGNORED_HMAC_EXCEPTION_CODES
Definition: AbstractExceptionHandler.php:53
‪TYPO3\CMS\Core\Error\AbstractExceptionHandler
Definition: AbstractExceptionHandler.php:37
‪TYPO3\CMS\Frontend\ContentObject\Exception\ExceptionHandlerInterface
Definition: ExceptionHandlerInterface.php:24
‪TYPO3\CMS\Frontend\ContentObject\Exception\ProductionExceptionHandler\handle
‪handle(\Exception $exception, AbstractContentObject $contentObject=null, $contentObjectConfiguration=[])
Definition: ProductionExceptionHandler.php:57
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:54
‪TYPO3\CMS\Frontend\ContentObject\Exception\ProductionExceptionHandler\__construct
‪__construct(protected Context $context, protected Random $random, protected LoggerInterface $logger, protected RequestId $requestId)
Definition: ProductionExceptionHandler.php:36
‪TYPO3\CMS\Frontend\ContentObject\Exception
Definition: ContentRenderingException.php:16
‪TYPO3\CMS\Core\Core\RequestId
Definition: RequestId.php:26
‪TYPO3\CMS\Frontend\ContentObject\Exception\ProductionExceptionHandler\$configuration
‪array $configuration
Definition: ProductionExceptionHandler.php:34
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject
Definition: AbstractContentObject.php:31
‪TYPO3\CMS\Core\Error\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Crypto\Random
Definition: Random.php:27
‪TYPO3\CMS\Core\Http\ImmediateResponseException
Definition: ImmediateResponseException.php:35
‪TYPO3\CMS\Frontend\ContentObject\Exception\ProductionExceptionHandler
Definition: ProductionExceptionHandler.php:33