Skip to content
This repository was archived by the owner on Jun 8, 2022. It is now read-only.

Commit a280953

Browse files
authored
Merge pull request #17 from SebKay/error-handling
Error handling
2 parents eb5fb85 + 4c370f1 commit a280953

File tree

7 files changed

+162
-12
lines changed

7 files changed

+162
-12
lines changed
Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
{% extends 'base.twig' %}
22

33
{% block body %}
4-
<h2>
5-
{{ code }} - {{ description }}
6-
</h2>
4+
<div class="txt-center">
5+
<h2>
6+
{{ code }}
7+
{% if description %}
8+
-
9+
{{ description }}
10+
{% endif %}
11+
</h2>
712

8-
<p>
9-
Please get in touch if this problem persists.
10-
</p>
13+
<br>
14+
15+
<p>
16+
Please get in touch if this problem persists.
17+
</p>
18+
</div>
1119
{% endblock %}

src/App.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Psr\Http\Message\ServerRequestInterface;
1010
use Slim\App as SlimApp;
1111
use DI\Bridge\Slim\Bridge as AppFactory;
12+
use Slim\Handlers\ErrorHandler as SlimErrorHandler;
13+
use Slim\Middleware\ErrorMiddleware;
1214

1315
class App
1416
{
@@ -27,6 +29,11 @@ class App
2729
*/
2830
protected $slim;
2931

32+
/**
33+
* @var ErrorMiddleware
34+
*/
35+
protected $error_middleware;
36+
3037
/**
3138
* @var Logger
3239
*/
@@ -95,14 +102,29 @@ protected function addMiddleware(): void
95102

96103
$this->slim()->add($this->container()->get('csrf'));
97104

98-
$this->slim()->addErrorMiddleware(
105+
$this->error_middleware = $this->slim()->addErrorMiddleware(
99106
$this->isDevelopmentMode(),
100107
true,
101108
true,
102109
$this->logger
103110
);
104111
}
105112

113+
/**
114+
* Add error rendering
115+
*/
116+
protected function addErrorRenderers(): void
117+
{
118+
$error_handler = $this->error_middleware->getDefaultErrorHandler();
119+
120+
if ($error_handler instanceof SlimErrorHandler) {
121+
$error_handler->registerErrorRenderer(
122+
'text/html',
123+
$this->container()->get('error-renderer-html')
124+
);
125+
}
126+
}
127+
106128
/**
107129
* Define routes
108130
*/
@@ -117,6 +139,7 @@ protected function addRoutes(): void
117139
protected function setupSlim(): void
118140
{
119141
$this->addMiddleware();
142+
$this->addErrorRenderers();
120143
$this->addRoutes();
121144
}
122145

src/Container/ContainerCreator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ protected function services(): array
2727
{
2828
return [
2929
LoggerService::class,
30+
ErrorRendererHTMLService::class,
3031
CSRFService::class,
3132
ViewService::class
3233
];
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Container;
4+
5+
use App\ErrorRendererHTML;
6+
7+
class ErrorRendererHTMLService extends Service
8+
{
9+
public static function name(): string
10+
{
11+
return 'error-renderer-html';
12+
}
13+
14+
public function config(): string
15+
{
16+
return ErrorRendererHTML::class;
17+
}
18+
}

src/Controllers/Controller.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ abstract class Controller
2323
*/
2424
protected $csrf;
2525

26-
/**
27-
* Set up
28-
*
29-
* @param Container $container
30-
*/
3126
public function __construct(Container $container)
3227
{
3328
$this->container = $container;

src/ErrorRendererHTML.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use App\Dependencies\View;
6+
use DI\Container;
7+
use Slim\Interfaces\ErrorRendererInterface;
8+
9+
class ErrorRendererHTML implements ErrorRendererInterface
10+
{
11+
/**
12+
* @var Container
13+
*/
14+
protected $container;
15+
16+
/**
17+
* @var View
18+
*/
19+
protected $view;
20+
21+
public function __construct(Container $container)
22+
{
23+
$this->container = $container;
24+
$this->view = $this->container->get('view');
25+
}
26+
27+
public function __invoke(\Throwable $exception, bool $displayErrorDetails): string
28+
{
29+
return $this->view->render('layouts/http-error.twig', [
30+
'code' => $exception->getCode(),
31+
'description' => Helpers::getHttpStatusMessage($exception->getCode()),
32+
]);
33+
}
34+
}

src/Helpers.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,75 @@ public static function generateCSRFData(Guard $csrf_guard, ServerRequestInterfac
2828
'value' => $request->getAttribute($value_key)
2929
];
3030
}
31+
32+
public static function httpStatuses(): array
33+
{
34+
return [
35+
100 => 'Continue',
36+
101 => 'Switching Protocols',
37+
102 => 'Processing',
38+
103 => 'Checkpoint',
39+
200 => 'OK',
40+
201 => 'Created',
41+
202 => 'Accepted',
42+
203 => 'Non-Authoritative Information',
43+
204 => 'No Content',
44+
205 => 'Reset Content',
45+
206 => 'Partial Content',
46+
207 => 'Multi-Status',
47+
300 => 'Multiple Choices',
48+
301 => 'Moved Permanently',
49+
302 => 'Found',
50+
303 => 'See Other',
51+
304 => 'Not Modified',
52+
305 => 'Use Proxy',
53+
306 => 'Switch Proxy',
54+
307 => 'Temporary Redirect',
55+
400 => 'Bad Request',
56+
401 => 'Unauthorized',
57+
402 => 'Payment Required',
58+
403 => 'Forbidden',
59+
404 => 'Not Found',
60+
405 => 'Method Not Allowed',
61+
406 => 'Not Acceptable',
62+
407 => 'Proxy Authentication Required',
63+
408 => 'Request Timeout',
64+
409 => 'Conflict',
65+
410 => 'Gone',
66+
411 => 'Length Required',
67+
412 => 'Precondition Failed',
68+
413 => 'Request Entity Too Large',
69+
414 => 'Request-URI Too Long',
70+
415 => 'Unsupported Media Type',
71+
416 => 'Requested Range Not Satisfiable',
72+
417 => 'Expectation Failed',
73+
418 => "I'm a teapot",
74+
422 => 'Unprocessable Entity',
75+
423 => 'Locked',
76+
424 => 'Failed Dependency',
77+
425 => 'Unordered Collection',
78+
426 => 'Upgrade Required',
79+
449 => 'Retry With',
80+
450 => 'Blocked by Windows Parental Controls',
81+
500 => 'Internal Server Error',
82+
501 => 'Not Implemented',
83+
502 => 'Bad Gateway',
84+
503 => 'Service Unavailable',
85+
504 => 'Gateway Timeout',
86+
505 => 'HTTP Version Not Supported',
87+
506 => 'Variant Also Negotiates',
88+
507 => 'Insufficient Storage',
89+
509 => 'Bandwidth Limit Exceeded',
90+
510 => 'Not Extended'
91+
];
92+
}
93+
94+
/**
95+
* @param int|string $code
96+
* @return string
97+
*/
98+
public static function getHttpStatusMessage($code): string
99+
{
100+
return (string) (self::httpStatuses()[$code] ?? '');
101+
}
31102
}

0 commit comments

Comments
 (0)