1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* Slim Framework (https://slimframework.com)
|
|
|
5 |
*
|
|
|
6 |
* @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License)
|
|
|
7 |
*/
|
|
|
8 |
|
|
|
9 |
declare(strict_types=1);
|
|
|
10 |
|
|
|
11 |
namespace Slim\Error\Renderers;
|
|
|
12 |
|
|
|
13 |
use Slim\Error\AbstractErrorRenderer;
|
|
|
14 |
use Throwable;
|
|
|
15 |
|
|
|
16 |
use function get_class;
|
|
|
17 |
use function htmlentities;
|
|
|
18 |
use function sprintf;
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* Default Slim application HTML Error Renderer
|
|
|
22 |
*/
|
|
|
23 |
class HtmlErrorRenderer extends AbstractErrorRenderer
|
|
|
24 |
{
|
|
|
25 |
public function __invoke(Throwable $exception, bool $displayErrorDetails): string
|
|
|
26 |
{
|
|
|
27 |
if ($displayErrorDetails) {
|
|
|
28 |
$html = '<p>The application could not run because of the following error:</p>';
|
|
|
29 |
$html .= '<h2>Details</h2>';
|
|
|
30 |
$html .= $this->renderExceptionFragment($exception);
|
|
|
31 |
} else {
|
|
|
32 |
$html = "<p>{$this->getErrorDescription($exception)}</p>";
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
return $this->renderHtmlBody($this->getErrorTitle($exception), $html);
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
private function renderExceptionFragment(Throwable $exception): string
|
|
|
39 |
{
|
|
|
40 |
$html = sprintf('<div><strong>Type:</strong> %s</div>', get_class($exception));
|
|
|
41 |
|
|
|
42 |
/** @var int|string $code */
|
|
|
43 |
$code = $exception->getCode();
|
|
|
44 |
$html .= sprintf('<div><strong>Code:</strong> %s</div>', $code);
|
|
|
45 |
|
|
|
46 |
$html .= sprintf('<div><strong>Message:</strong> %s</div>', htmlentities($exception->getMessage()));
|
|
|
47 |
|
|
|
48 |
$html .= sprintf('<div><strong>File:</strong> %s</div>', $exception->getFile());
|
|
|
49 |
|
|
|
50 |
$html .= sprintf('<div><strong>Line:</strong> %s</div>', $exception->getLine());
|
|
|
51 |
|
|
|
52 |
$html .= '<h2>Trace</h2>';
|
|
|
53 |
$html .= sprintf('<pre>%s</pre>', htmlentities($exception->getTraceAsString()));
|
|
|
54 |
|
|
|
55 |
return $html;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
public function renderHtmlBody(string $title = '', string $html = ''): string
|
|
|
59 |
{
|
|
|
60 |
return sprintf(
|
|
|
61 |
'<!doctype html>' .
|
|
|
62 |
'<html lang="en">' .
|
|
|
63 |
' <head>' .
|
|
|
64 |
' <meta charset="utf-8">' .
|
|
|
65 |
' <meta name="viewport" content="width=device-width, initial-scale=1">' .
|
|
|
66 |
' <title>%s</title>' .
|
|
|
67 |
' <style>' .
|
|
|
68 |
' body{margin:0;padding:30px;font:12px/1.5 Helvetica,Arial,Verdana,sans-serif}' .
|
|
|
69 |
' h1{margin:0;font-size:48px;font-weight:normal;line-height:48px}' .
|
|
|
70 |
' strong{display:inline-block;width:65px}' .
|
|
|
71 |
' </style>' .
|
|
|
72 |
' </head>' .
|
|
|
73 |
' <body>' .
|
|
|
74 |
' <h1>%s</h1>' .
|
|
|
75 |
' <div>%s</div>' .
|
|
|
76 |
' <a href="#" onclick="window.history.go(-1)">Go Back</a>' .
|
|
|
77 |
' </body>' .
|
|
|
78 |
'</html>',
|
|
|
79 |
$title,
|
|
|
80 |
$title,
|
|
|
81 |
$html
|
|
|
82 |
);
|
|
|
83 |
}
|
|
|
84 |
}
|