/
DebugViewFriendlyErrorFormatter.php
169 lines (146 loc) · 3.57 KB
/
DebugViewFriendlyErrorFormatter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace SilverStripe\Logging;
use SilverStripe\Core\Convert;
use SilverStripe\Dev\Debug;
use SilverStripe\Control\Director;
use SilverStripe\Control\Email\Email;
use Monolog\Formatter\FormatterInterface;
use Monolog\LogRecord;
/**
* Produce a friendly error message
*/
class DebugViewFriendlyErrorFormatter implements FormatterInterface
{
/**
* Default status code
*
* @var int
*/
protected $statusCode = 500;
/**
* Default friendly error
*
* @var string
*/
protected $friendlyErrorMessage = 'Error';
/**
* Default error body
*
* @var string
*/
protected $friendlyErrorDetail;
/**
* Get default status code
*
* @return int
*/
public function getStatusCode()
{
return $this->statusCode;
}
/**
* Set default status code
*
* @param int $statusCode
* @return $this
*/
public function setStatusCode($statusCode)
{
$this->statusCode = $statusCode;
return $this;
}
/**
* Get friendly title
*
* @return string
*/
public function getTitle()
{
return $this->friendlyErrorMessage;
}
/**
* Set friendly title
*
* @param string $title
* @return $this
*/
public function setTitle($title)
{
$this->friendlyErrorMessage = $title;
return $this;
}
/**
* Get default error body
*
* @return string
*/
public function getBody()
{
return $this->friendlyErrorDetail;
}
/**
* Set default error body
*
* @param string $body
* @return $this
*/
public function setBody($body)
{
$this->friendlyErrorDetail = $body;
return $this;
}
public function format(array|LogRecord $record)
{
// Get error code
$code = empty($record['code']) ? $this->getStatusCode() : $record['code'];
return $this->output($code);
}
public function formatBatch(array $records)
{
$message = '';
foreach ($records as $record) {
$message .= $this->format($record);
}
return $message;
}
/**
* Return the appropriate error content for the given status code
*
* @param int $statusCode
* @return string Content in an appropriate format for the current request
*/
public function output($statusCode)
{
if (Director::is_ajax()) {
return $this->getTitle();
}
$renderer = Debug::create_debug_view();
$output = $renderer->renderHeader();
$output .= $renderer->renderInfo("Website Error", $this->getTitle(), $this->getBody());
if (!is_null($contactInfo = $this->addContactAdministratorInfo())) {
$output .= $renderer->renderParagraph($contactInfo);
}
$output .= $renderer->renderFooter();
return $output;
}
/**
* Generate the line with admin contact info
*
* @return string|null
*/
private function addContactAdministratorInfo()
{
if (!$adminEmail = Email::config()->admin_email) {
return null;
}
if (is_string($adminEmail)) {
return 'Contact an administrator: ' . Email::obfuscate($adminEmail);
}
if (!is_array($adminEmail) || !count($adminEmail ?? [])) {
return null;
}
$email = array_keys($adminEmail)[0];
$name = array_values($adminEmail)[0];
return sprintf('Contact %s: %s', Convert::raw2xml($name), Email::obfuscate($email));
}
}