‪TYPO3CMS  11.5
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;
26 
32 {
33  protected array ‪$configuration = [];
34 
36  protected ‪Random ‪$random;
37  protected LoggerInterface ‪$logger;
38 
40  {
41  $this->context = ‪$context;
42  $this->random = ‪$random;
43  $this->logger = ‪$logger;
44  }
45 
46  public function ‪setConfiguration(array ‪$configuration): void
47  {
48  $this->configuration = ‪$configuration;
49  }
50 
62  public function ‪handle(\‪Exception $exception, ‪AbstractContentObject $contentObject = null, $contentObjectConfiguration = []): string
63  {
64  // ImmediateResponseException (and the derived PropagateResponseException) should work similar to
65  // exit / die and must therefore not be handled by this ExceptionHandler.
66  if ($exception instanceof ‪ImmediateResponseException) {
67  throw $exception;
68  }
69 
70  if (!empty($this->configuration['ignoreCodes.'])
71  && in_array($exception->getCode(), array_map('intval', $this->configuration['ignoreCodes.']), true)
72  ) {
73  throw $exception;
74  }
75 
76  $errorMessage = $this->configuration['errorMessage'] ?? 'Oops, an error occurred! Code: {code}';
77  $code = $this->context->getAspect('date')->getDateTime()->format('YmdHis') . $this->random->generateRandomHexString(8);
78 
79  // "%s" has to be replaced by {code} for b/w compatibility
80  $errorMessage = str_replace('%s', '{code}', $errorMessage);
81 
82  // Log exception except HMAC validation exceptions caused by potentially forged requests
83  if (!in_array($exception->getCode(), ‪AbstractExceptionHandler::IGNORED_HMAC_EXCEPTION_CODES, true)) {
84  $this->logger->alert($errorMessage, ['exception' => $exception, 'code' => $code]);
85  }
86 
87  // Return error message by replacing {code} with the actual code, generated above
88  return str_replace('{code}', $code, $errorMessage);
89  }
90 }
‪TYPO3\CMS\Frontend\ContentObject\Exception\ProductionExceptionHandler\setConfiguration
‪setConfiguration(array $configuration)
Definition: ProductionExceptionHandler.php:46
‪TYPO3\CMS\Frontend\ContentObject\Exception\ProductionExceptionHandler\$random
‪Random $random
Definition: ProductionExceptionHandler.php:36
‪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\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Frontend\ContentObject\Exception
Definition: ContentRenderingException.php:16
‪TYPO3\CMS\Frontend\ContentObject\Exception\ProductionExceptionHandler\$context
‪Context $context
Definition: ProductionExceptionHandler.php:35
‪TYPO3\CMS\Frontend\ContentObject\Exception\ProductionExceptionHandler\$logger
‪LoggerInterface $logger
Definition: ProductionExceptionHandler.php:37
‪TYPO3\CMS\Frontend\ContentObject\Exception\ProductionExceptionHandler\handle
‪string handle(\Exception $exception, AbstractContentObject $contentObject=null, $contentObjectConfiguration=[])
Definition: ProductionExceptionHandler.php:62
‪TYPO3\CMS\Frontend\ContentObject\Exception\ProductionExceptionHandler\$configuration
‪array $configuration
Definition: ProductionExceptionHandler.php:33
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject
Definition: AbstractContentObject.php:29
‪TYPO3\CMS\Core\Error\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Crypto\Random
Definition: Random.php:24
‪TYPO3\CMS\Core\Http\ImmediateResponseException
Definition: ImmediateResponseException.php:35
‪TYPO3\CMS\Frontend\ContentObject\Exception\ProductionExceptionHandler
Definition: ProductionExceptionHandler.php:32
‪TYPO3\CMS\Frontend\ContentObject\Exception\ProductionExceptionHandler\__construct
‪__construct(Context $context, Random $random, LoggerInterface $logger)
Definition: ProductionExceptionHandler.php:39