‪TYPO3CMS  9.5
ProductionExceptionHandler.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
17 use Psr\Log\LoggerAwareInterface;
18 use Psr\Log\LoggerAwareTrait;
23 
28 class ‪ProductionExceptionHandler implements ‪ExceptionHandlerInterface, LoggerAwareInterface
29 {
30  use LoggerAwareTrait;
31 
35  protected ‪$configuration = [];
36 
40  public function ‪__construct(array ‪$configuration = [])
41  {
42  $this->configuration = ‪$configuration;
43  }
44 
56  public function ‪handle(\‪Exception $exception, ‪AbstractContentObject $contentObject = null, $contentObjectConfiguration = [])
57  {
58  // ImmediateResponseException should work similar to exit / die and must therefore not be handled by this ExceptionHandler.
59  if ($exception instanceof ‪ImmediateResponseException) {
60  throw $exception;
61  }
62 
63  if (!empty($this->configuration['ignoreCodes.'])) {
64  if (in_array($exception->getCode(), array_map('intval', $this->configuration['ignoreCodes.']), true)) {
65  throw $exception;
66  }
67  }
68  $errorMessage = $this->configuration['errorMessage'] ?? 'Oops, an error occurred! Code: %s';
69  $code = date('YmdHis', $_SERVER['REQUEST_TIME']) . GeneralUtility::makeInstance(Random::class)->generateRandomHexString(8);
70 
71  $this->‪logException($exception, $errorMessage, $code);
72 
73  return sprintf($errorMessage, $code);
74  }
75 
81  protected function ‪logException(\‪Exception $exception, $errorMessage, $code)
82  {
83  $this->logger->alert(sprintf($errorMessage, $code), ['exception' => $exception]);
84  }
85 }
‪TYPO3\CMS\Frontend\ContentObject\Exception\ProductionExceptionHandler\__construct
‪__construct(array $configuration=[])
Definition: ProductionExceptionHandler.php:39
‪TYPO3\CMS\Frontend\ContentObject\Exception\ExceptionHandlerInterface
Definition: ExceptionHandlerInterface.php:23
‪TYPO3\CMS\Frontend\ContentObject\Exception
Definition: ContentRenderingException.php:2
‪TYPO3\CMS\Frontend\ContentObject\Exception\ProductionExceptionHandler\handle
‪string handle(\Exception $exception, AbstractContentObject $contentObject=null, $contentObjectConfiguration=[])
Definition: ProductionExceptionHandler.php:55
‪TYPO3\CMS\Frontend\ContentObject\Exception\ProductionExceptionHandler\logException
‪logException(\Exception $exception, $errorMessage, $code)
Definition: ProductionExceptionHandler.php:80
‪TYPO3\CMS\Frontend\ContentObject\Exception\ProductionExceptionHandler\$configuration
‪array $configuration
Definition: ProductionExceptionHandler.php:34
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject
Definition: AbstractContentObject.php:24
‪TYPO3\CMS\Core\Resource\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Crypto\Random
Definition: Random.php:22
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Http\ImmediateResponseException
Definition: ImmediateResponseException.php:29
‪TYPO3\CMS\Frontend\ContentObject\Exception\ProductionExceptionHandler
Definition: ProductionExceptionHandler.php:29