404 and CustomErrorHandler

I am registering a custom error handler:

$errorMiddleware = $app->addErrorMiddleware(false, false, false);

$errorHandler = $errorMiddleware->getDefaultErrorHandler();
$errorHandler->registerErrorRenderer('text/html', MyCustomErrorRenderer::class);

And I have a custom 404 handler:

class CustomNotFoundErrorRenderer extends HttpSpecializedException {
...
}

But when I do

throw new CustomNotFoundErrorRenderer($request);

my 404 class does not work, but outputs what it returns MyCustomErrorRenderer.

I’m already confused about who intercepts when and what. I need my own error handler and my own 404 page. Please help me.

It’s OK! I used the code recommended in the Upgrade section (I hadn’t looked into this section before as a complete idiot :man_facepalming:)

// Set the Not Found Handler
$errorMiddleware->setErrorHandler(
    HttpNotFoundException::class,
    function (ServerRequestInterface $request, Throwable $exception, bool $displayErrorDetails) {
        $response = new Slim\Psr7\Response();
        $response->getBody()->write('404 NOT FOUND');
        return $response->withStatus(404);
    });

Now everything is working as it should! :upside_down_face: