Creating a custom error handler in TYPO3 9

The config.yaml needs to look like this:

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

and the file ErrorHandling.php:

<?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()) {
            DebuggerUtility::var_dump($message);
            DebuggerUtility::var_dump($reasons);
            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.