f1853b6302b930b8e4e634180ff6dd8c88f6e1d2
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / ContentObject / Exception / ProductionExceptionHandler.php
1 <?php
2 namespace TYPO3\CMS\Frontend\ContentObject\Exception;
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;
19 use TYPO3\CMS\Core\Crypto\Random;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Frontend\ContentObject\AbstractContentObject;
22
23 /**
24 * Exception handler class for content object rendering
25 */
26 class ProductionExceptionHandler implements ExceptionHandlerInterface, LoggerAwareInterface
27 {
28 use LoggerAwareTrait;
29
30 /**
31 * @var array
32 */
33 protected $configuration = [];
34
35 /**
36 * @param array $configuration
37 */
38 public function __construct(array $configuration = [])
39 {
40 $this->configuration = $configuration;
41 }
42
43 /**
44 * Handles exceptions thrown during rendering of content objects
45 * The handler can decide whether to re-throw the exception or
46 * return a nice error message for production context.
47 *
48 * @param \Exception $exception
49 * @param AbstractContentObject $contentObject
50 * @param array $contentObjectConfiguration
51 * @return string
52 * @throws \Exception
53 */
54 public function handle(\Exception $exception, AbstractContentObject $contentObject = null, $contentObjectConfiguration = [])
55 {
56 if (!empty($this->configuration['ignoreCodes.'])) {
57 if (in_array($exception->getCode(), array_map('intval', $this->configuration['ignoreCodes.']), true)) {
58 throw $exception;
59 }
60 }
61 $errorMessage = isset($this->configuration['errorMessage']) ? $this->configuration['errorMessage'] : 'Oops, an error occurred! Code: %s';
62 $code = date('YmdHis', $_SERVER['REQUEST_TIME']) . GeneralUtility::makeInstance(Random::class)->generateRandomHexString(8);
63
64 $this->logException($exception, $errorMessage, $code);
65
66 return sprintf($errorMessage, $code);
67 }
68
69 /**
70 * @param \Exception $exception
71 * @param string $errorMessage
72 * @param string $code
73 */
74 protected function logException(\Exception $exception, $errorMessage, $code)
75 {
76 $this->logger->alert(sprintf($errorMessage, $code), ['exception' => $exception]);
77 }
78 }