‪TYPO3CMS  ‪main
ErrorController.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\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
32 
38 {
45  public function ‪internalErrorAction(ServerRequestInterface $request, string $message, array $reasons = []): ResponseInterface
46  {
47  if (!$this->‪isPageUnavailableHandlerConfigured($request)) {
48  throw new ‪InternalServerErrorException($message, 1607585445);
49  }
50  $errorHandler = $this->‪getErrorHandlerFromSite($request, 500);
51  if ($errorHandler !== null) {
52  return $errorHandler->handlePageError($request, $message, $reasons);
53  }
54  return $this->‪handleDefaultError($request, 500, $message ?: 'Internal Server Error');
55  }
56 
63  public function ‪unavailableAction(ServerRequestInterface $request, string $message, array $reasons = []): ResponseInterface
64  {
65  if (!$this->‪isPageUnavailableHandlerConfigured($request)) {
66  throw new ‪ServiceUnavailableException($message, 1518472181);
67  }
68  $errorHandler = $this->‪getErrorHandlerFromSite($request, 503);
69  if ($errorHandler !== null) {
70  return $errorHandler->handlePageError($request, $message, $reasons);
71  }
72  return $this->‪handleDefaultError($request, 503, $message ?: 'Service Unavailable');
73  }
74 
81  public function ‪pageNotFoundAction(ServerRequestInterface $request, string $message, array $reasons = []): ResponseInterface
82  {
83  $errorHandler = $this->‪getErrorHandlerFromSite($request, 404);
84  if ($errorHandler !== null) {
85  return $errorHandler->handlePageError($request, $message, $reasons);
86  }
87  try {
88  return $this->‪handleDefaultError($request, 404, $message);
89  } catch (\RuntimeException $e) {
90  throw new ‪PageNotFoundException($message, 1518472189);
91  }
92  }
93 
100  public function ‪accessDeniedAction(ServerRequestInterface $request, string $message, array $reasons = []): ResponseInterface
101  {
102  $errorHandler = $this->‪getErrorHandlerFromSite($request, 403);
103  if ($errorHandler !== null) {
104  return $errorHandler->handlePageError($request, $message, $reasons);
105  }
106  try {
107  return $this->‪handleDefaultError($request, 403, $message);
108  } catch (\RuntimeException $e) {
109  throw new ‪PageNotFoundException($message, 1518472195);
110  }
111  }
112 
119  protected function ‪isPageUnavailableHandlerConfigured(ServerRequestInterface $request): bool
120  {
121  $normalizedParams = $request->getAttribute('normalizedParams');
122  return !‪GeneralUtility::cmpIP($normalizedParams->getRemoteAddress(), ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask']);
123  }
124 
128  protected function ‪getErrorHandlerFromSite(ServerRequestInterface $request, int ‪$statusCode): ?‪PageErrorHandlerInterface
129  {
130  $site = $request->getAttribute('site');
131  if ($site instanceof ‪Site) {
132  try {
133  return $site->getErrorHandler(‪$statusCode);
135  // No error handler found, so fallback back to the generic TYPO3 error handler.
136  }
137  }
138  return null;
139  }
140 
144  protected function ‪handleDefaultError(ServerRequestInterface $request, int ‪$statusCode, string $reason = ''): ResponseInterface
145  {
146  if (str_contains($request->getHeaderLine('Accept'), 'application/json')) {
147  return new ‪JsonResponse(['reason' => $reason], ‪$statusCode);
148  }
149  $content = GeneralUtility::makeInstance(ErrorPageController::class)->errorAction(
150  'Page Not Found',
151  'The page did not exist or was inaccessible.' . ($reason ? ' Reason: ' . $reason : ''),
152  0,
154  );
155  return new ‪HtmlResponse($content, ‪$statusCode);
156  }
157 }
‪TYPO3\CMS\Core\Controller\ErrorPageController
Definition: ErrorPageController.php:30
‪TYPO3\CMS\Frontend\Controller\ErrorController\internalErrorAction
‪internalErrorAction(ServerRequestInterface $request, string $message, array $reasons=[])
Definition: ErrorController.php:45
‪TYPO3\CMS\Frontend\Controller\ErrorController\pageNotFoundAction
‪pageNotFoundAction(ServerRequestInterface $request, string $message, array $reasons=[])
Definition: ErrorController.php:81
‪TYPO3\CMS\Frontend\Controller\ErrorController\isPageUnavailableHandlerConfigured
‪bool isPageUnavailableHandlerConfigured(ServerRequestInterface $request)
Definition: ErrorController.php:119
‪TYPO3\CMS\Core\Utility\GeneralUtility\cmpIP
‪static bool cmpIP(string $baseIP, string $list)
Definition: GeneralUtility.php:113
‪TYPO3\CMS\Core\Error\PageErrorHandler\PageErrorHandlerNotConfiguredException
Definition: PageErrorHandlerNotConfiguredException.php:26
‪TYPO3\CMS\Frontend\Controller\ErrorController
Definition: ErrorController.php:38
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:42
‪TYPO3\CMS\Redirects\Message\$statusCode
‪identifier readonly UriInterface readonly int $statusCode
Definition: RedirectWasHitMessage.php:34
‪TYPO3\CMS\Core\Error\Http\PageNotFoundException
Definition: PageNotFoundException.php:24
‪TYPO3\CMS\Core\Error\Http\InternalServerErrorException
Definition: InternalServerErrorException.php:24
‪TYPO3\CMS\Frontend\Controller\ErrorController\handleDefaultError
‪handleDefaultError(ServerRequestInterface $request, int $statusCode, string $reason='')
Definition: ErrorController.php:144
‪TYPO3\CMS\Frontend\Controller\ErrorController\unavailableAction
‪unavailableAction(ServerRequestInterface $request, string $message, array $reasons=[])
Definition: ErrorController.php:63
‪TYPO3\CMS\Core\Error\Http\ServiceUnavailableException
Definition: ServiceUnavailableException.php:24
‪TYPO3\CMS\Core\Error\PageErrorHandler\PageErrorHandlerInterface
Definition: PageErrorHandlerInterface.php:29
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:28
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Frontend\Controller\ErrorController\accessDeniedAction
‪accessDeniedAction(ServerRequestInterface $request, string $message, array $reasons=[])
Definition: ErrorController.php:100
‪TYPO3\CMS\Frontend\Controller
Definition: ErrorController.php:18
‪TYPO3\CMS\Frontend\Controller\ErrorController\getErrorHandlerFromSite
‪getErrorHandlerFromSite(ServerRequestInterface $request, int $statusCode)
Definition: ErrorController.php:128
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:28