Skip to content

Commit

Permalink
Error view is required
Browse files Browse the repository at this point in the history
  • Loading branch information
msmakouz committed Jun 23, 2022
1 parent 11f5f6e commit 99a8bca
Showing 1 changed file with 5 additions and 16 deletions.
21 changes: 5 additions & 16 deletions app/src/ErrorHandler/ViewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Spiral\Http\ErrorHandler\RendererInterface;
use Spiral\Http\Header\AcceptHeader;
use Spiral\Views\Exception\ViewException;
use Spiral\Views\ViewInterface;
use Spiral\Views\ViewsInterface;

class ViewRenderer implements RendererInterface
Expand Down Expand Up @@ -47,26 +46,16 @@ private function renderJson(int $code, string $message): ResponseInterface
private function renderView(int $code, string $message): ResponseInterface
{
$response = $this->responseFactory->createResponse($code);
$content = "Error code: {$code}";

$view = $this->findTemplate(\sprintf(self::VIEW, $code));
$view ??= $this->findTemplate(self::GENERAL_VIEW);

if ($view) {
$content = $view->render(['code' => $code, 'error' => $message]);
try {
$view = $this->views->get(\sprintf(self::VIEW, $code));
} catch (ViewException) {
$view = $this->views->get(self::GENERAL_VIEW);
}

$content = $view->render(['code' => $code, 'error' => $message]);
$response->getBody()->write($content);

return $response;
}

private function findTemplate(string $path): ?ViewInterface
{
try {
return $this->views->get($path);
} catch (ViewException $e) {
return null;
}
}
}

0 comments on commit 99a8bca

Please sign in to comment.