Newer
Older
catch
committed
<?php
/**
* @file
* Contains \Drupal\Core\Utility\Error.
*/
namespace Drupal\Core\Utility;
use Drupal\Component\Utility\String;
use Drupal\Component\Utility\Xss;
use Drupal\Component\Utility\SafeMarkup;
catch
committed
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
/**
* Drupal error utility class.
*/
class Error {
/**
* The error severity level.
*
* @var int
*/
const ERROR = 3;
/**
* An array of blacklisted functions.
*
* @var array
*/
protected static $blacklistFunctions = array('debug', '_drupal_error_handler', '_drupal_exception_handler');
/**
* Decodes an exception and retrieves the correct caller.
*
* @param \Exception $exception
* The exception object that was thrown.
*
* @return array
* An error in the format expected by _drupal_log_error().
*/
public static function decodeException(\Exception $exception) {
$message = $exception->getMessage();
$backtrace = $exception->getTrace();
// Add the line throwing the exception to the backtrace.
array_unshift($backtrace, array('line' => $exception->getLine(), 'file' => $exception->getFile()));
// For PDOException errors, we try to return the initial caller,
// skipping internal functions of the database layer.
if ($exception instanceof \PDOException) {
// The first element in the stack is the call, the second element gives us
// the caller. We skip calls that occurred in one of the classes of the
// database layer or in one of its global functions.
$db_functions = array('db_query', 'db_query_range');
while (!empty($backtrace[1]) && ($caller = $backtrace[1]) &&
((isset($caller['class']) && (strpos($caller['class'], 'Query') !== FALSE || strpos($caller['class'], 'Database') !== FALSE || strpos($caller['class'], 'PDO') !== FALSE)) ||
in_array($caller['function'], $db_functions))) {
// We remove that call.
array_shift($backtrace);
}
if (isset($exception->query_string, $exception->args)) {
$message .= ": " . $exception->query_string . "; " . print_r($exception->args, TRUE);
}
}
$caller = static::getLastCaller($backtrace);
return array(
'%type' => get_class($exception),
// The standard PHP exception handler considers that the exception message
// is plain-text. We mimic this behavior here.
'!message' => String::checkPlain($message),
'%function' => $caller['function'],
'%file' => $caller['file'],
'%line' => $caller['line'],
'severity_level' => static::ERROR,
'backtrace' => $backtrace,
);
}
/**
* Renders an exception error message without further exceptions.
*
* @param \Exception $exception
* The exception object that was thrown.
*
* @return string
* An error message.
*/
public static function renderExceptionSafe(\Exception $exception) {
$decode = static::decodeException($exception);
$backtrace = $decode['backtrace'];
catch
committed
unset($decode['backtrace']);
// Remove 'main()'.
array_shift($backtrace);
catch
committed
$output = String::format('%type: !message in %function (line %line of %file).', $decode);
// Even though it is possible that this method is called on a public-facing
// site, it is only called when the exception handler itself threw an
// exception, which normally means that a code change caused the system to
// no longer function correctly (as opposed to a user-triggered error), so
// we assume that it is safe to include a verbose backtrace.
$output .= '<pre>' . static::formatBacktrace($backtrace) . '</pre>';
return SafeMarkup::set($output);
catch
committed
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
}
/**
* Gets the last caller from a backtrace.
*
* @param array $backtrace
* A standard PHP backtrace. Passed by reference.
*
* @return array
* An associative array with keys 'file', 'line' and 'function'.
*/
public static function getLastCaller(array &$backtrace) {
// Errors that occur inside PHP internal functions do not generate
// information about file and line. Ignore black listed functions.
while (($backtrace && !isset($backtrace[0]['line'])) ||
(isset($backtrace[1]['function']) && in_array($backtrace[1]['function'], static::$blacklistFunctions))) {
array_shift($backtrace);
}
// The first trace is the call itself.
// It gives us the line and the file of the last call.
$call = $backtrace[0];
// The second call gives us the function where the call originated.
if (isset($backtrace[1])) {
if (isset($backtrace[1]['class'])) {
$call['function'] = $backtrace[1]['class'] . $backtrace[1]['type'] . $backtrace[1]['function'] . '()';
}
else {
$call['function'] = $backtrace[1]['function'] . '()';
}
}
else {
$call['function'] = 'main()';
}
return $call;
}
/**
* Formats a backtrace into a plain-text string.
*
* The calls show values for scalar arguments and type names for complex ones.
*
* @param array $backtrace
* A standard PHP backtrace.
*
* @return string
* A plain-text line-wrapped string ready to be put inside <pre>.
*/
public static function formatBacktrace(array $backtrace) {
$return = '';
foreach ($backtrace as $trace) {
$call = array('function' => '', 'args' => array());
if (isset($trace['class'])) {
$call['function'] = $trace['class'] . $trace['type'] . $trace['function'];
}
elseif (isset($trace['function'])) {
$call['function'] = $trace['function'];
}
else {
$call['function'] = 'main';
}
if (isset($trace['args'])) {
foreach ($trace['args'] as $arg) {
if (is_scalar($arg)) {
$call['args'][] = is_string($arg) ? '\'' . Xss::filter($arg) . '\'' : $arg;
}
else {
$call['args'][] = ucfirst(gettype($arg));
}
}
}
$return .= $call['function'] . '(' . implode(', ', $call['args']) . ")\n";
}
return $return;
}
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
* Formats a flattened backtrace into a plain-text string.
*
* The calls show values for scalar arguments and type names for complex ones.
*
* @param array $backtrace
* The backtrace of a Symfony\Component\Debug\Exception\FlattenException.
*
* @return string
* A plain-text line-wrapped string ready to be put inside <pre>.
*/
public static function formatFlattenedBacktrace(array $backtrace) {
$return = '';
foreach ($backtrace as $trace) {
$call = array('function' => '', 'args' => array());
if (isset($trace['class'])) {
$call['function'] = $trace['class'] . $trace['type'] . $trace['function'];
}
elseif (isset($trace['function'])) {
$call['function'] = $trace['function'];
}
else {
$call['function'] = 'main';
}
if (isset($trace['args'])) {
foreach ($trace['args'] as $arg) {
$type = $arg[0];
$value = $arg[1];
if ($type == 'array') {
$call['args'][] = '[' . ucfirst($type) . ']';
}
elseif ($type == 'null') {
$call['args'][] = strtoupper($type);
}
elseif ($type == 'boolean') {
$call['args'][] = $value ? 'TRUE' : 'FALSE';
}
else {
$call['args'][] = $value;
}
}
}
$return .= $call['function'] . '(' . implode(', ', $call['args']) . ")\n";
}
return $return;
}