|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tempest\Router\Exceptions; |
| 4 | + |
| 5 | +use Tempest\Auth\Exceptions\AccessWasDenied; |
| 6 | +use Tempest\Container\Container; |
| 7 | +use Tempest\Core\AppConfig; |
| 8 | +use Tempest\Http\GenericResponse; |
| 9 | +use Tempest\Http\HttpRequestFailed; |
| 10 | +use Tempest\Http\Request; |
| 11 | +use Tempest\Http\Response; |
| 12 | +use Tempest\Http\Responses\Invalid; |
| 13 | +use Tempest\Http\Session\CsrfTokenDidNotMatch; |
| 14 | +use Tempest\Http\Status; |
| 15 | +use Tempest\Router\MatchedRoute; |
| 16 | +use Tempest\Support\Filesystem; |
| 17 | +use Tempest\Validation\Exceptions\ValidationFailed; |
| 18 | +use Tempest\View\GenericView; |
| 19 | +use Throwable; |
| 20 | +use Whoops\Handler\PrettyPageHandler; |
| 21 | +use Whoops\Run; |
| 22 | + |
| 23 | +final readonly class HtmlExceptionRenderer |
| 24 | +{ |
| 25 | + public function __construct( |
| 26 | + private AppConfig $appConfig, |
| 27 | + private Container $container, |
| 28 | + ) {} |
| 29 | + |
| 30 | + public function render(Throwable $throwable): Response |
| 31 | + { |
| 32 | + if ($throwable instanceof ConvertsToResponse) { |
| 33 | + return $throwable->toResponse(); |
| 34 | + } |
| 35 | + |
| 36 | + if ($this->shouldRenderDevelopmentException($throwable)) { |
| 37 | + $whoops = $this->createHandler(); |
| 38 | + |
| 39 | + return new GenericResponse( |
| 40 | + status: Status::INTERNAL_SERVER_ERROR, |
| 41 | + body: $whoops->handleException($throwable), |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + return match (true) { |
| 46 | + $throwable instanceof ValidationFailed => new Invalid($throwable->subject, $throwable->failingRules, $throwable->targetClass), |
| 47 | + $throwable instanceof RouteBindingFailed => $this->renderErrorResponse(Status::NOT_FOUND), |
| 48 | + $throwable instanceof AccessWasDenied => $this->renderErrorResponse(Status::FORBIDDEN), |
| 49 | + $throwable instanceof HttpRequestFailed => $this->renderErrorResponse($throwable->status, $throwable), |
| 50 | + $throwable instanceof CsrfTokenDidNotMatch => $this->renderErrorResponse(Status::UNPROCESSABLE_CONTENT), |
| 51 | + default => $this->renderErrorResponse(Status::INTERNAL_SERVER_ERROR, $throwable), |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + private function renderErrorResponse(Status $status, ?Throwable $exception = null): Response |
| 56 | + { |
| 57 | + if ($exception instanceof HttpRequestFailed && $exception->cause?->body) { |
| 58 | + return $exception->cause; |
| 59 | + } |
| 60 | + |
| 61 | + return new GenericResponse( |
| 62 | + status: $status, |
| 63 | + body: new GenericView(__DIR__ . '/html/error.view.php', [ |
| 64 | + 'css' => $this->getStyleSheet(), |
| 65 | + 'status' => $status->value, |
| 66 | + 'title' => $status->description(), |
| 67 | + 'message' => $exception?->getMessage() ?: match ($status) { |
| 68 | + Status::INTERNAL_SERVER_ERROR => 'An unexpected server error occurred', |
| 69 | + Status::NOT_FOUND => 'This page could not be found on the server', |
| 70 | + Status::FORBIDDEN => 'You do not have permission to access this page', |
| 71 | + Status::UNAUTHORIZED => 'You must be authenticated in to access this page', |
| 72 | + Status::UNPROCESSABLE_CONTENT => 'The request could not be processed due to invalid data', |
| 73 | + default => null, |
| 74 | + }, |
| 75 | + ]), |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + private function getStyleSheet(): string |
| 80 | + { |
| 81 | + return Filesystem\read_file(__DIR__ . '/html/style.css'); |
| 82 | + } |
| 83 | + |
| 84 | + private function shouldRenderDevelopmentException(Throwable $throwable): bool |
| 85 | + { |
| 86 | + if (! $this->appConfig->environment->isLocal()) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + if (! $throwable instanceof HttpRequestFailed) { |
| 91 | + return true; |
| 92 | + } |
| 93 | + |
| 94 | + if ($throwable->status === Status::NOT_FOUND) { |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + return true; |
| 99 | + } |
| 100 | + |
| 101 | + private function createHandler(): Run |
| 102 | + { |
| 103 | + $handler = new PrettyPageHandler(); |
| 104 | + |
| 105 | + $handler->addDataTableCallback('Route', function () { |
| 106 | + $route = $this->container->get(MatchedRoute::class); |
| 107 | + |
| 108 | + if (! $route) { |
| 109 | + return []; |
| 110 | + } |
| 111 | + |
| 112 | + return [ |
| 113 | + 'Handler' => $route->route->handler->getDeclaringClass()->getFileName() . ':' . $route->route->handler->getName(), |
| 114 | + 'URI' => $route->route->uri, |
| 115 | + 'Allowed parameters' => $route->route->parameters, |
| 116 | + 'Received parameters' => $route->params, |
| 117 | + ]; |
| 118 | + }); |
| 119 | + |
| 120 | + $handler->addDataTableCallback('Request', function () { |
| 121 | + $request = $this->container->get(Request::class); |
| 122 | + |
| 123 | + return [ |
| 124 | + 'URI' => $request->uri, |
| 125 | + 'Method' => $request->method->value, |
| 126 | + 'Headers' => $request->headers->toArray(), |
| 127 | + 'Parsed body' => array_filter(array_values($request->body)) ? $request->body : [], |
| 128 | + 'Raw body' => $request->raw, |
| 129 | + ]; |
| 130 | + }); |
| 131 | + |
| 132 | + $whoops = new Run(); |
| 133 | + $whoops->pushHandler($handler); |
| 134 | + |
| 135 | + return $whoops; |
| 136 | + } |
| 137 | +} |
0 commit comments