Creating a custom error handler in TYPO3 9

The config.yaml needs to look like this:

    errorCode: '404'
    errorHandler: PHP
    errorPhpClassFQCN: Ophi\Blog\Error\ErrorHandling

and the file ErrorHandling.php:


namespace Ophi\Blog\Error;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Error\PageErrorHandler\PageErrorHandlerInterface;
use TYPO3\CMS\Core\Http\HtmlResponse;
use TYPO3\CMS\Core\Http\RedirectResponse;
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
use Ophi\Blog\Service\Debug;

class ErrorHandling implements PageErrorHandlerInterface{

    public function handlePageError(ServerRequestInterface $request, string $message, array $reasons = []): ResponseInterface{
        if(Debug::isTestServer()) {
            return new HtmlResponse('Diese Seite liefert einen 404 Error', 404);
        return new RedirectResponse('/404-fehlerseite', 404);

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.