|
1
|
|
|
<?php |
|
2
|
|
|
namespace Slim\Handlers\Renderables; |
|
3
|
|
|
|
|
4
|
|
|
use Slim\Interfaces\RenderableInterface; |
|
5
|
|
|
|
|
6
|
|
|
class HtmlPhpErrorMessage implements RenderableInterface |
|
7
|
|
|
{ |
|
8
|
|
|
|
|
9
|
|
|
private $displayErrorDetails; |
|
10
|
|
|
|
|
11
|
|
|
public function render($args) |
|
12
|
|
|
{ |
|
13
|
|
|
$this->displayErrorDetails = $args[1]; |
|
14
|
|
|
return $this->renderHtmlPhpErrorMessage($args[0]); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
View Code Duplication |
protected function renderHtmlPhpErrorMessage(\Throwable $error) |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
$title = 'Slim Application Error'; |
|
20
|
|
|
|
|
21
|
|
|
if ($this->displayErrorDetails) { |
|
22
|
|
|
$html = '<p>The application could not run because of the following error:</p>'; |
|
23
|
|
|
$html .= '<h2>Details</h2>'; |
|
24
|
|
|
$html .= $this->renderHtmlError($error); |
|
25
|
|
|
|
|
26
|
|
|
while ($error = $error->getPrevious()) { |
|
27
|
|
|
$html .= '<h2>Previous error</h2>'; |
|
28
|
|
|
$html .= $this->renderHtmlError($error); |
|
29
|
|
|
} |
|
30
|
|
|
} else { |
|
31
|
|
|
$html = '<p>A website error has occurred. Sorry for the temporary inconvenience.</p>'; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$output = sprintf( |
|
35
|
|
|
"<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'>" . |
|
36
|
|
|
"<title>%s</title><style>body{margin:0;padding:30px;font:12px/1.5 Helvetica,Arial,Verdana," . |
|
37
|
|
|
"sans-serif;}h1{margin:0;font-size:48px;font-weight:normal;line-height:48px;}strong{" . |
|
38
|
|
|
"display:inline-block;width:65px;}</style></head><body><h1>%s</h1>%s</body></html>", |
|
39
|
|
|
$title, |
|
40
|
|
|
$title, |
|
41
|
|
|
$html |
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
|
|
return $output; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Render error as HTML. |
|
49
|
|
|
* |
|
50
|
|
|
* @param \Throwable $error |
|
51
|
|
|
* |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function renderHtmlError(\Throwable $error) |
|
55
|
|
|
{ |
|
56
|
|
|
$html = sprintf('<div><strong>Type:</strong> %s</div>', get_class($error)); |
|
57
|
|
|
|
|
58
|
|
|
if (($code = $error->getCode())) { |
|
59
|
|
|
$html .= sprintf('<div><strong>Code:</strong> %s</div>', $code); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if (($message = $error->getMessage())) { |
|
63
|
|
|
$html .= sprintf('<div><strong>Message:</strong> %s</div>', htmlentities($message)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if (($file = $error->getFile())) { |
|
67
|
|
|
$html .= sprintf('<div><strong>File:</strong> %s</div>', $file); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if (($line = $error->getLine())) { |
|
71
|
|
|
$html .= sprintf('<div><strong>Line:</strong> %s</div>', $line); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
View Code Duplication |
if (($trace = $error->getTraceAsString())) { |
|
|
|
|
|
|
75
|
|
|
$html .= '<h2>Trace</h2>'; |
|
76
|
|
|
$html .= sprintf('<pre>%s</pre>', htmlentities($trace)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $html; |
|
80
|
|
|
} |
|
81
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.