HtmlErrorRenderer.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace App\Exception\Handlers\Renderers;
  3. use App\Exceptions\UnderMaintenanceException;
  4. use Slim\Exception\HttpForbiddenException;
  5. use Slim\Exception\HttpMethodNotAllowedException;
  6. use Slim\Exception\HttpNotFoundException;
  7. use Slim\Exception\HttpUnauthorizedException;
  8. use Slim\Interfaces\ErrorRendererInterface;
  9. use Throwable;
  10. class HtmlErrorRenderer implements ErrorRendererInterface
  11. {
  12. /**
  13. * @param Throwable $exception
  14. * @param bool $displayErrorDetails
  15. * @return string
  16. * @throws \Twig\Error\LoaderError
  17. * @throws \Twig\Error\RuntimeError
  18. * @throws \Twig\Error\SyntaxError
  19. */
  20. public function __invoke(Throwable $exception, bool $displayErrorDetails): string
  21. {
  22. if ($exception instanceof UnderMaintenanceException) {
  23. return view()->string( 'errors/maintenance.twig');
  24. }
  25. if ($exception instanceof HttpUnauthorizedException || $exception instanceof HttpForbiddenException) {
  26. return view()->string( 'errors/403.twig');
  27. }
  28. if ($exception instanceof HttpMethodNotAllowedException) {
  29. return view()->string( 'errors/405.twig');
  30. }
  31. if ($exception instanceof HttpNotFoundException) {
  32. return view()->string( 'errors/404.twig');
  33. }
  34. return view()->string('errors/500.twig', ['exception' => $displayErrorDetails ? $exception : null]);
  35. }
  36. }