-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from msmakouz/feature/errors-handling-improvem…
…ents Adding ExceptionHandlerBootloader, ViewRenderer, error views
- Loading branch information
Showing
15 changed files
with
230 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Bootloader; | ||
|
||
use App\ErrorHandler\ViewRenderer; | ||
use App\Exception\CollisionRenderer; | ||
use Spiral\Boot\AbstractKernel; | ||
use Spiral\Boot\Bootloader\Bootloader; | ||
use Spiral\Exceptions\ExceptionHandler; | ||
use Spiral\Exceptions\ExceptionHandlerInterface; | ||
use Spiral\Exceptions\Renderer\JsonRenderer; | ||
use Spiral\Exceptions\Reporter\LoggerReporter; | ||
use Spiral\Exceptions\Reporter\SnapshotterReporter; | ||
use Spiral\Http\ErrorHandler\RendererInterface; | ||
use Spiral\Http\Middleware\ErrorHandlerMiddleware\EnvSuppressErrors; | ||
use Spiral\Http\Middleware\ErrorHandlerMiddleware\SuppressErrorsInterface; | ||
use Spiral\Ignition\IgnitionRenderer; | ||
|
||
final class ExceptionHandlerBootloader extends Bootloader | ||
{ | ||
protected const BINDINGS = [ | ||
SuppressErrorsInterface::class => EnvSuppressErrors::class, | ||
RendererInterface::class => ViewRenderer::class, | ||
]; | ||
|
||
public function init(AbstractKernel $kernel): void | ||
{ | ||
$kernel->running(static function (ExceptionHandler $handler): void { | ||
// $handler->addRenderer(new ConsoleRenderer()); | ||
$handler->addRenderer(new CollisionRenderer()); | ||
$handler->addRenderer(new JsonRenderer()); | ||
}); | ||
} | ||
|
||
public function boot( | ||
ExceptionHandlerInterface $handler, | ||
LoggerReporter $logger, | ||
SnapshotterReporter $snapshotter, | ||
IgnitionRenderer $ignition | ||
): void { | ||
$handler->addReporter($logger); | ||
$handler->addReporter($snapshotter); | ||
|
||
$handler->addRenderer($ignition); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\ErrorHandler; | ||
|
||
use Psr\Http\Message\ResponseFactoryInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface as Request; | ||
use Spiral\Http\ErrorHandler\RendererInterface; | ||
use Spiral\Http\Header\AcceptHeader; | ||
use Spiral\Views\Exception\ViewException; | ||
use Spiral\Views\ViewsInterface; | ||
|
||
class ViewRenderer implements RendererInterface | ||
{ | ||
private const GENERAL_VIEW = 'exception/error'; | ||
private const VIEW = 'exception/%s'; | ||
|
||
public function __construct( | ||
private readonly ViewsInterface $views, | ||
private readonly ResponseFactoryInterface $responseFactory | ||
) { | ||
} | ||
|
||
public function renderException(Request $request, int $code, string $message): ResponseInterface | ||
{ | ||
$acceptItems = AcceptHeader::fromString($request->getHeaderLine('Accept'))->getAll(); | ||
if ($acceptItems && $acceptItems[0]->getValue() === 'application/json') { | ||
return $this->renderJson($code, $message); | ||
} | ||
|
||
return $this->renderView($code, $message); | ||
} | ||
|
||
private function renderJson(int $code, string $message): ResponseInterface | ||
{ | ||
$response = $this->responseFactory->createResponse($code); | ||
|
||
$response = $response->withHeader('Content-Type', 'application/json; charset=UTF-8'); | ||
$response->getBody()->write(\json_encode(['status' => $code, 'error' => $message])); | ||
|
||
return $response; | ||
} | ||
|
||
private function renderView(int $code, string $message): ResponseInterface | ||
{ | ||
$response = $this->responseFactory->createResponse($code); | ||
|
||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<extends:layout.base title="[[Access Forbidden]]"/> | ||
<use:element path="embed/links" as="homepage:links"/> | ||
|
||
<stack:push name="styles"> | ||
<link rel="stylesheet" href="/styles/welcome.css"/> | ||
</stack:push> | ||
|
||
<define:body> | ||
<div class="wrapper"> | ||
<div class="placeholder"> | ||
<img src="/images/403.svg" alt="Error 403" width="300px"/> | ||
<h2>[[Access Forbidden]]</h2> | ||
<homepage:links git="https://github.com/spiral/app" style="font-weight: bold;"/> | ||
<div style="font-size: 12px; margin-top: 10px;"> | ||
[[This view file is located in]] <b>app/views/exception/403.dark.php</b>. | ||
</div> | ||
</div> | ||
</div> | ||
</define:body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<extends:layout.base title="[[Page not found]]"/> | ||
<use:element path="embed/links" as="homepage:links"/> | ||
|
||
<stack:push name="styles"> | ||
<link rel="stylesheet" href="/styles/welcome.css"/> | ||
</stack:push> | ||
|
||
<define:body> | ||
<div class="wrapper"> | ||
<div class="placeholder"> | ||
<img src="/images/404.svg" alt="Framework Logotype" width="300px"/> | ||
<h2>[[Page not found]]</h2> | ||
<homepage:links git="https://github.com/spiral/app" style="font-weight: bold;"/> | ||
<div style="font-size: 12px; margin-top: 10px;"> | ||
[[This view file is located in]] <b>app/views/exception/404.dark.php</b>. | ||
</div> | ||
</div> | ||
</div> | ||
</define:body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<extends:layout.base title="[[Something went wrong]]"/> | ||
<use:element path="embed/links" as="homepage:links"/> | ||
|
||
<stack:push name="styles"> | ||
<link rel="stylesheet" href="/styles/welcome.css"/> | ||
</stack:push> | ||
|
||
<define:body> | ||
<div class="wrapper"> | ||
<div class="placeholder"> | ||
<img src="/images/500.svg" alt="Error 500" width="300px"/> | ||
<h2>[[Something went wrong]]</h2> | ||
<homepage:links git="https://github.com/spiral/app" style="font-weight: bold;"/> | ||
<div style="font-size: 12px; margin-top: 10px;"> | ||
[[This view file is located in]] <b>app/views/exception/500.dark.php</b>. | ||
</div> | ||
</div> | ||
</div> | ||
</define:body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<extends:layout.base title="[[Something went wrong]]"/> | ||
<use:element path="embed/links" as="homepage:links"/> | ||
|
||
<stack:push name="styles"> | ||
<link rel="stylesheet" href="/styles/welcome.css"/> | ||
</stack:push> | ||
|
||
<define:body> | ||
<div class="wrapper"> | ||
<div class="placeholder"> | ||
<img src="/images/logo.svg" alt="Framework Logotype" width="200px"/> | ||
<h2>[[Something went wrong]]. [[Error code]]: {{ $code }}</h2> | ||
<homepage:links git="https://github.com/spiral/app" style="font-weight: bold;"/> | ||
<div style="font-size: 12px; margin-top: 10px;"> | ||
[[This view file is located in]] <b>app/views/exception/error.dark.php</b>. | ||
</div> | ||
</div> | ||
</div> | ||
</define:body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.