[TASK] Display exact error message of handlePageError()
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Error / PageErrorHandler / PageContentErrorHandler.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Core\Error\PageErrorHandler;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use Psr\Http\Message\ResponseInterface;
20 use Psr\Http\Message\ServerRequestInterface;
21 use TYPO3\CMS\Core\Exception\SiteNotFoundException;
22 use TYPO3\CMS\Core\Http\HtmlResponse;
23 use TYPO3\CMS\Core\LinkHandling\LinkService;
24 use TYPO3\CMS\Core\Routing\InvalidRouteArgumentsException;
25 use TYPO3\CMS\Core\Site\Entity\Site;
26 use TYPO3\CMS\Core\Site\SiteFinder;
27 use TYPO3\CMS\Core\Utility\GeneralUtility;
28
29 /**
30 * Renders the content of a page to be displayed (also in relation to language etc)
31 * This is typically configured via the "Sites configuration" module in the backend.
32 */
33 class PageContentErrorHandler implements PageErrorHandlerInterface
34 {
35
36 /**
37 * @var int
38 */
39 protected $statusCode;
40
41 /**
42 * @var array
43 */
44 protected $errorHandlerConfiguration;
45
46 /**
47 * PageContentErrorHandler constructor.
48 * @param int $statusCode
49 * @param array $configuration
50 * @throws \InvalidArgumentException
51 */
52 public function __construct(int $statusCode, array $configuration)
53 {
54 $this->statusCode = $statusCode;
55 if (empty($configuration['errorContentSource'])) {
56 throw new \InvalidArgumentException('PageContentErrorHandler needs to have a proper link set.', 1522826413);
57 }
58 $this->errorHandlerConfiguration = $configuration;
59 }
60
61 /**
62 * @param ServerRequestInterface $request
63 * @param string $message
64 * @param array $reasons
65 * @return ResponseInterface
66 * @throws \RuntimeException
67 */
68 public function handlePageError(ServerRequestInterface $request, string $message, array $reasons = []): ResponseInterface
69 {
70 try {
71 $resolvedUrl = $this->resolveUrl($request, $this->errorHandlerConfiguration['errorContentSource']);
72 $content = null;
73 $report = [];
74 if ($resolvedUrl !== (string)$request->getUri()) {
75 $content = GeneralUtility::getUrl($resolvedUrl, 0, null, $report);
76 if ($content === false && ((int)$report['error'] === -1 || (int)$report['error'] > 200)) {
77 throw new \RuntimeException('Error handler could not fetch error page "' . $resolvedUrl . '", reason: ' . $report['message'], 1544172838);
78 }
79 }
80 } catch (InvalidRouteArgumentsException | SiteNotFoundException $e) {
81 $content = 'Invalid error handler configuration: ' . $this->errorHandlerConfiguration['errorContentSource'];
82 }
83 return new HtmlResponse($content, $this->statusCode);
84 }
85
86 /**
87 * Resolve the URL (currently only page and external URL are supported)
88 *
89 * @param ServerRequestInterface $request
90 * @param string $typoLinkUrl
91 * @return string
92 * @throws SiteNotFoundException
93 * @throws InvalidRouteArgumentsException
94 */
95 protected function resolveUrl(ServerRequestInterface $request, string $typoLinkUrl): string
96 {
97 $linkService = GeneralUtility::makeInstance(LinkService::class);
98 $urlParams = $linkService->resolve($typoLinkUrl);
99 if ($urlParams['type'] !== 'page' && $urlParams['type'] !== 'url') {
100 throw new \InvalidArgumentException('PageContentErrorHandler can only handle TYPO3 urls of types "page" or "url"', 1522826609);
101 }
102 if ($urlParams['type'] === 'url') {
103 return $urlParams['url'];
104 }
105
106 $site = $request->getAttribute('site', null);
107 if (!$site instanceof Site) {
108 $site = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId((int)$urlParams['pageuid']);
109 }
110 // Build Url
111 return (string)$site->getRouter()->generateUri(
112 (int)$urlParams['pageuid'],
113 ['_language' => $request->getAttribute('language', null)]
114 );
115 }
116 }