Error handler no longer handles errors in Slim 4.15

Upgrading from 4.14.0 to 4.15.0 breaks custom error handlers for me. I configured error handler 2 or 3 years ago to have proper status codes and custom HTML pages for common HTTP errors. In 4.15, unknown URLs trigger 500 Internal Server Error and, in dev environment, show a stack trace.

<br />
<b>Fatal error</b>:  Uncaught Slim\Exception\HttpNotFoundException: Not found. in C:\REDACTED\example.com\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php:76
Stack trace:
#0 C:\REDACTED\example.com\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php(44): Slim\Middleware\RoutingMiddleware-&gt;performRouting(Object(Slim\Psr7\Request))
#1 C:\REDACTED\example.com\vendor\slim\slim\Slim\MiddlewareDispatcher.php(129): Slim\Middleware\RoutingMiddleware-&gt;process(Object(Slim\Psr7\Request), Object(Psr\Http\Server\RequestHandlerInterface@anonymous))
#2 C:\REDACTED\example.com\vendor\slim\slim\Slim\MiddlewareDispatcher.php(73): Psr\Http\Server\RequestHandlerInterface@anonymous-&gt;handle(Object(Slim\Psr7\Request))
#3 C:\REDACTED\example.com\vendor\slim\slim\Slim\App.php(209): Slim\MiddlewareDispatcher-&gt;handle(Object(Slim\Psr7\Request))
#4 C:\REDACTED\example.com\vendor\slim\slim\Slim\App.php(193): Slim\App-&gt;handle(Object(Slim\Psr7\Request))
#5 C:\REDACTED\example.com\public\index.php(53): Slim\App-&gt;run()
#6 {main}
  thrown in <b>C:\REDACTED\example.com\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php</b> on line <b>76</b><br />

Step debugging shows that error handler is populated correctly (\Slim\Middleware\ErrorMiddleware::$handlers) but handler is never called.

I may have done something wrong when I first configured it, because this part of the framework is not really something I fully understand.

  1. I’ve created a custom class to set up middleware which I invoke in index.php:

    $app = AppFactory::create();
    $middlewareConfigurator = new MiddlewareConfigurator();
    $middlewareConfigurator->add($app);
    
  2. My class calls a private method to configure some error handlers:

     private function configureErrorMiddleware(App $app, ErrorMiddleware $errorMiddleware): ErrorMiddleware
     {
         $errorMiddleware->setErrorHandler(HttpNotFoundException::class, function (
             Request $request,
             \Throwable $exception,
             bool $displayErrorDetails,
             bool $logErrors,
             bool $logErrorDetails
         ) use ($app) {
             /** @var Config $config */
             $config = $app->getContainer();
    
             return (new Error($config))
                 ->notFound404($request, $app->getResponseFactory()->createResponse());
         });
         // Etc.
    

Am I doing something wrong, or it’s a legit bug?

Would you mind raising an issue here GitHub · Where software is built?
Ideally, with a minimal reproducible case in a single index.php file.

It doesn’t happen when I do something similar in an empty project, perhaps it’s a clash with some other package. I need to investigate.

I found the problem. I upgraded dependencies and added a fix to handle trailing slashes on the same day. While trying to figure out why my fix didn’t work, I moved the line of code to add routing middleware to the bottom. As the comment in the error middleware section warns:

This middleware should be added last. It will not handle any exceptions/errors for middleware added after it.

Slim upgrade was a red herring. When I reverted to an earlier version, I also reverted my wrong move in middleware configuration.

Sorry!