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