‪TYPO3CMS  11.5
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;
33 
39 {
50  public function ‪internalErrorAction(ServerRequestInterface $request, string $message, array $reasons = []): ResponseInterface
51  {
53  throw new ‪InternalServerErrorException($message, 1607585445);
54  }
55  $errorHandler = $this->‪getErrorHandlerFromSite($request, 500);
56  if ($errorHandler instanceof ‪PageErrorHandlerInterface) {
57  return $errorHandler->handlePageError($request, $message, $reasons);
58  }
59  return $this->‪handleDefaultError($request, 500, $message ?: 'Internal Server Error');
60  }
61 
72  public function ‪unavailableAction(ServerRequestInterface $request, string $message, array $reasons = []): ResponseInterface
73  {
75  throw new ‪ServiceUnavailableException($message, 1518472181);
76  }
77  $errorHandler = $this->‪getErrorHandlerFromSite($request, 503);
78  if ($errorHandler instanceof ‪PageErrorHandlerInterface) {
79  return $errorHandler->handlePageError($request, $message, $reasons);
80  }
81  return $this->‪handleDefaultError($request, 503, $message ?: 'Service Unavailable');
82  }
83 
94  public function ‪pageNotFoundAction(ServerRequestInterface $request, string $message, array $reasons = []): ResponseInterface
95  {
96  $errorHandler = $this->‪getErrorHandlerFromSite($request, 404);
97  if ($errorHandler instanceof ‪PageErrorHandlerInterface) {
98  return $errorHandler->handlePageError($request, $message, $reasons);
99  }
100  try {
101  return $this->‪handleDefaultError($request, 404, $message);
102  } catch (\RuntimeException $e) {
103  throw new ‪PageNotFoundException($message, 1518472189);
104  }
105  }
106 
117  public function ‪accessDeniedAction(ServerRequestInterface $request, string $message, array $reasons = []): ResponseInterface
118  {
119  $errorHandler = $this->‪getErrorHandlerFromSite($request, 403);
120  if ($errorHandler instanceof ‪PageErrorHandlerInterface) {
121  return $errorHandler->handlePageError($request, $message, $reasons);
122  }
123  try {
124  return $this->‪handleDefaultError($request, 403, $message);
125  } catch (\RuntimeException $e) {
126  throw new ‪PageNotFoundException($message, 1518472195);
127  }
128  }
129 
136  protected function ‪isPageUnavailableHandlerConfigured(): bool
137  {
138  return !GeneralUtility::cmpIP(GeneralUtility::getIndpEnv('REMOTE_ADDR'), ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask']);
139  }
140 
148  protected function ‪getErrorHandlerFromSite(ServerRequestInterface $request, int $statusCode): ?‪PageErrorHandlerInterface
149  {
150  $site = $request->getAttribute('site');
151  if ($site instanceof ‪Site) {
152  try {
153  return $site->getErrorHandler($statusCode);
155  // No error handler found, so fallback back to the generic TYPO3 error handler.
156  }
157  }
158  return null;
159  }
160 
169  protected function ‪handleDefaultError(ServerRequestInterface $request, int $statusCode, string $reason = ''): ResponseInterface
170  {
171  if (str_contains($request->getHeaderLine('Accept'), 'application/json')) {
172  return new ‪JsonResponse(['reason' => $reason], $statusCode);
173  }
174  $content = GeneralUtility::makeInstance(ErrorPageController::class)->errorAction(
175  'Page Not Found',
176  'The page did not exist or was inaccessible.' . ($reason ? ' Reason: ' . $reason : ''),
178  0,
179  $statusCode
180  );
181  return new ‪HtmlResponse($content, $statusCode);
182  }
183 }
‪TYPO3\CMS\Core\Messaging\AbstractMessage
Definition: AbstractMessage.php:26
‪TYPO3\CMS\Core\Controller\ErrorPageController
Definition: ErrorPageController.php:30
‪TYPO3\CMS\Frontend\Controller\ErrorController\unavailableAction
‪ResponseInterface unavailableAction(ServerRequestInterface $request, string $message, array $reasons=[])
Definition: ErrorController.php:72
‪TYPO3\CMS\Core\Error\PageErrorHandler\PageErrorHandlerNotConfiguredException
Definition: PageErrorHandlerNotConfiguredException.php:26
‪TYPO3\CMS\Frontend\Controller\ErrorController\accessDeniedAction
‪ResponseInterface accessDeniedAction(ServerRequestInterface $request, string $message, array $reasons=[])
Definition: ErrorController.php:117
‪TYPO3\CMS\Frontend\Controller\ErrorController
Definition: ErrorController.php:39
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:42
‪TYPO3\CMS\Frontend\Controller\ErrorController\internalErrorAction
‪ResponseInterface internalErrorAction(ServerRequestInterface $request, string $message, array $reasons=[])
Definition: ErrorController.php:50
‪TYPO3\CMS\Core\Error\Http\PageNotFoundException
Definition: PageNotFoundException.php:24
‪TYPO3\CMS\Frontend\Controller\ErrorController\handleDefaultError
‪ResponseInterface handleDefaultError(ServerRequestInterface $request, int $statusCode, string $reason='')
Definition: ErrorController.php:169
‪TYPO3\CMS\Core\Error\Http\InternalServerErrorException
Definition: InternalServerErrorException.php:24
‪TYPO3\CMS\Core\Error\Http\ServiceUnavailableException
Definition: ServiceUnavailableException.php:24
‪TYPO3\CMS\Core\Error\PageErrorHandler\PageErrorHandlerInterface
Definition: PageErrorHandlerInterface.php:29
‪TYPO3\CMS\Frontend\Controller\ErrorController\isPageUnavailableHandlerConfigured
‪bool isPageUnavailableHandlerConfigured()
Definition: ErrorController.php:136
‪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
Definition: ErrorController.php:18
‪TYPO3\CMS\Frontend\Controller\ErrorController\getErrorHandlerFromSite
‪PageErrorHandlerInterface null getErrorHandlerFromSite(ServerRequestInterface $request, int $statusCode)
Definition: ErrorController.php:148
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Frontend\Controller\ErrorController\pageNotFoundAction
‪ResponseInterface pageNotFoundAction(ServerRequestInterface $request, string $message, array $reasons=[])
Definition: ErrorController.php:94
‪TYPO3\CMS\Core\Messaging\AbstractMessage\ERROR
‪const ERROR
Definition: AbstractMessage.php:31
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:26