Error Handling Middleware

    You can now map custom handlers for any type of Exception or Throwable.

    1. use Psr\Http\Message\ServerRequestInterface;
    2. use Slim\Factory\AppFactory;
    3. use Slim\Psr7\Response;
    4. require __DIR__ . '/../vendor/autoload.php';
    5. $app = AppFactory::create();
    6. // Add Routing Middleware
    7. $app->addRoutingMiddleware();
    8. // Define Custom Error Handler
    9. $customErrorHandler = function (
    10. ServerRequestInterface $request,
    11. Throwable $exception,
    12. bool $displayErrorDetails,
    13. bool $logErrors,
    14. bool $logErrorDetails
    15. ) use ($app) {
    16. $payload = ['error' => $exception->getMessage()];
    17. $response = $app->getResponseFactory()->createResponse();
    18. $response->getBody()->write(
    19. json_encode($payload, JSON_UNESCAPED_UNICODE)
    20. );
    21. };
    22. // Add Error Middleware
    23. $errorMiddleware = $app->addErrorMiddleware(true, true, true);
    24. $errorMiddleware->setDefaultErrorHandler($customErrorHandler);
    25. // ...
    26. $app->run();
    1. <?php
    2. use MyApp\Handlers\MyErrorHandler;
    3. use Slim\Factory\AppFactory;
    4. require __DIR__ . '/../vendor/autoload.php';
    5. $app = AppFactory::create();
    6. // Add Routing Middleware
    7. $app->addRoutingMiddleware();
    8. // Instantiate Your Custom Error Handler
    9. $myErrorHandler = new MyErrorHandler($app->getCallableResolver(), $app->getResponseFactory());
    10. // Add Error Middleware
    11. $errorMiddleware = $app->addErrorMiddleware(true, true, true);
    12. $errorMiddleware->setDefaultErrorHandler($myErrorHandler);
    13. // ...
    14. $app->run();

    The rendering is finally decoupled from the handling.It will still detect the content-type and render things appropriately with the help of ErrorRenderers.The core ErrorHandler extends the AbstractErrorHandler class which has been completely refactored.By default it will call the appropriate ErrorRenderer for the supported content types. The coreErrorHandler defines renderers for the following content types:

    • application/json
    • application/xml and text/xml
    • text/html
    1. <?php
    2. use MyApp\Handlers\MyErrorHandler;
    3. use Slim\Factory\AppFactory;
    4. require __DIR__ . '/../vendor/autoload.php';
    5. $app = AppFactory::create();
    6. // Add Routing Middleware
    7. $app->addRoutingMiddleware();
    8. // Add Error Middleware
    9. $errorMiddleware = $app->addErrorMiddleware(true, true, true);
    10. // Get the default error handler and register my custom error renderer.
    11. $errorHandler = $errorMiddleware->getDefaultErrorHandler();
    12. $errorHandler->registerErrorRenderer('text/html', MyCustomErrorRenderer::class);
    13. // ...
    14. $app->run();

    By default, the error handler tries to detect the error renderer using the Accept header of therequest. If you need to force the error handler to use a specific error renderer you can write the following.

    The base class HttpSpecializedException extends Exception and comes with the following sub classes:

    • HttpBadRequestException
    • HttpForbiddenException
    • HttpInternalServerErrorException
    • HttpNotAllowedException
    • HttpNotFoundException
    • HttpNotImplementedException
    • HttpUnauthorizedExceptionYou can extend the HttpSpecializedException class if they need any other response codes that we decide not to provide with the base repository. Example if you wanted a 504 gateway timeout exception that behaves like the native ones you would do the following:
    1. class HttpForbiddenException extends HttpException
    2. {
    3. protected $code = 504;
    4. protected $message = 'Gateway Timeout.';
    5. protected $title = '504 Gateway Timeout';