|
1
|
|
|
<?php |
|
2
|
|
|
namespace Slim\Handlers\Renderables; |
|
3
|
|
|
|
|
4
|
|
|
use Slim\Interfaces\RenderableInterface; |
|
5
|
|
|
|
|
6
|
|
|
class HtmlErrorMessage implements RenderableInterface |
|
7
|
|
|
{ |
|
8
|
|
|
|
|
9
|
|
|
private $displayErrorDetails; |
|
10
|
|
|
|
|
11
|
|
|
public function render($args) |
|
12
|
|
|
{ |
|
13
|
|
|
$this->displayErrorDetails = $args[1]; |
|
14
|
|
|
return $this->renderHtmlErrorMessage($args[0]); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Render HTML error page |
|
20
|
|
|
* |
|
21
|
|
|
* @param \Exception $exception |
|
22
|
|
|
* |
|
23
|
|
|
* @return string |
|
24
|
|
|
*/ |
|
25
|
|
View Code Duplication |
protected function renderHtmlErrorMessage(\Exception $exception) |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
$title = 'Slim Application Error'; |
|
28
|
|
|
|
|
29
|
|
|
if ($this->displayErrorDetails) { |
|
30
|
|
|
$html = '<p>The application could not run because of the following error:</p>'; |
|
31
|
|
|
$html .= '<h2>Details</h2>'; |
|
32
|
|
|
$html .= $this->renderHtmlException($exception); |
|
33
|
|
|
|
|
34
|
|
|
while ($exception = $exception->getPrevious()) { |
|
35
|
|
|
$html .= '<h2>Previous exception</h2>'; |
|
36
|
|
|
$html .= $this->renderHtmlExceptionOrError($exception); |
|
37
|
|
|
} |
|
38
|
|
|
} else { |
|
39
|
|
|
$html = '<p>A website error has occurred. Sorry for the temporary inconvenience.</p>'; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$output = sprintf( |
|
43
|
|
|
"<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'>" . |
|
44
|
|
|
"<title>%s</title><style>body{margin:0;padding:30px;font:12px/1.5 Helvetica,Arial,Verdana," . |
|
45
|
|
|
"sans-serif;}h1{margin:0;font-size:48px;font-weight:normal;line-height:48px;}strong{" . |
|
46
|
|
|
"display:inline-block;width:65px;}</style></head><body><h1>%s</h1>%s</body></html>", |
|
47
|
|
|
$title, |
|
48
|
|
|
$title, |
|
49
|
|
|
$html |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
|
|
return $output; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Render exception as HTML. |
|
57
|
|
|
* |
|
58
|
|
|
* Provided for backwards compatibility; use renderHtmlExceptionOrError(). |
|
59
|
|
|
* |
|
60
|
|
|
* @param \Exception $exception |
|
61
|
|
|
* |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function renderHtmlException(\Exception $exception) |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->renderHtmlExceptionOrError($exception); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Render exception or error as HTML. |
|
71
|
|
|
* |
|
72
|
|
|
* @param \Exception|\Error $exception |
|
73
|
|
|
* |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function renderHtmlExceptionOrError($exception) |
|
77
|
|
|
{ |
|
78
|
|
|
if (!$exception instanceof \Exception && !$exception instanceof \Error) { |
|
|
|
|
|
|
79
|
|
|
throw new \RuntimeException("Unexpected type. Expected Exception or Error."); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$html = sprintf('<div><strong>Type:</strong> %s</div>', get_class($exception)); |
|
83
|
|
|
|
|
84
|
|
|
if (($code = $exception->getCode())) { |
|
85
|
|
|
$html .= sprintf('<div><strong>Code:</strong> %s</div>', $code); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if (($message = $exception->getMessage())) { |
|
89
|
|
|
$html .= sprintf('<div><strong>Message:</strong> %s</div>', htmlentities($message)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (($file = $exception->getFile())) { |
|
93
|
|
|
$html .= sprintf('<div><strong>File:</strong> %s</div>', $file); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if (($line = $exception->getLine())) { |
|
97
|
|
|
$html .= sprintf('<div><strong>Line:</strong> %s</div>', $line); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
View Code Duplication |
if (($trace = $exception->getTraceAsString())) { |
|
|
|
|
|
|
101
|
|
|
$html .= '<h2>Trace</h2>'; |
|
102
|
|
|
$html .= sprintf('<pre>%s</pre>', htmlentities($trace)); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $html; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
} |
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.