Slim 4 - Error throw HttpNotFoundException

I set a custom error handler, but I have problem. When I open URL which is not support, app throw error which Uncaught:

Fatal error : Uncaught Slim\Exception\HttpNotFoundException: Not found.

Why? App working if add: try{}catch(){}; But it wrongs method…

Are there lines below in your code?

use Slim\Exception\HttpNotFoundException;
use Slim\Interfaces\ErrorRendererInterface;
use Throwable;

Look at an example from Rob Allen

Sounds like you did not add the ErrorMiddleware to your app.

$app->addErrorMiddleware(true, true, true);

self::$app->addErrorMiddleware(true, true, true)->setDefaultErrorHandler(ErrorHandler::class);
ErrorHandler.php

But i have error:
" Fatal error : Uncaught Slim\Exception\HttpNotFoundException: Not found. in G:\GitHub.Repositories\Slim4\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php:91

Yeah sure, it’s my handler error: HandlerError.php

Did you add the ErrorMiddleware as the very last middleware in your queue?

1 Like

Thanks.

It was:
self::$app->addErrorMiddleware(true, true, true)->setDefaultErrorHandler(ErrorHandler::class); self::$app->addRoutingMiddleware();

Update:
self::$app->addRoutingMiddleware(); self::$app->addErrorMiddleware(true, true, true)->setDefaultErrorHandler(ErrorHandler::class);