5 * Functions for error handling.
8 use Drupal\Component\Render\FormattableMarkup;
9 use Drupal\Component\Utility\Xss;
10 use Drupal\Core\Logger\RfcLogLevel;
11 use Drupal\Core\Render\Markup;
12 use Drupal\Core\Utility\Error;
13 use Symfony\Component\HttpFoundation\Response;
16 * Maps PHP error constants to watchdog severity levels.
18 * The error constants are documented at
19 * http://php.net/manual/errorfunc.constants.php
21 * @ingroup logging_severity_levels
23 function drupal_error_levels() {
25 E_ERROR => ['Error', RfcLogLevel::ERROR],
26 E_WARNING => ['Warning', RfcLogLevel::WARNING],
27 E_PARSE => ['Parse error', RfcLogLevel::ERROR],
28 E_NOTICE => ['Notice', RfcLogLevel::NOTICE],
29 E_CORE_ERROR => ['Core error', RfcLogLevel::ERROR],
30 E_CORE_WARNING => ['Core warning', RfcLogLevel::WARNING],
31 E_COMPILE_ERROR => ['Compile error', RfcLogLevel::ERROR],
32 E_COMPILE_WARNING => ['Compile warning', RfcLogLevel::WARNING],
33 E_USER_ERROR => ['User error', RfcLogLevel::ERROR],
34 E_USER_WARNING => ['User warning', RfcLogLevel::WARNING],
35 E_USER_NOTICE => ['User notice', RfcLogLevel::NOTICE],
36 E_STRICT => ['Strict warning', RfcLogLevel::DEBUG],
37 E_RECOVERABLE_ERROR => ['Recoverable fatal error', RfcLogLevel::ERROR],
38 E_DEPRECATED => ['Deprecated function', RfcLogLevel::DEBUG],
39 E_USER_DEPRECATED => ['User deprecated function', RfcLogLevel::DEBUG],
46 * Provides custom PHP error handling.
49 * The level of the error raised.
53 * The filename that the error was raised in.
55 * The line number the error was raised at.
57 * An array that points to the active symbol table at the point the error
60 function _drupal_error_handler_real($error_level, $message, $filename, $line, $context) {
61 if ($error_level & error_reporting()) {
62 $types = drupal_error_levels();
63 list($severity_msg, $severity_level) = $types[$error_level];
64 $backtrace = debug_backtrace();
65 $caller = Error::getLastCaller($backtrace);
67 // We treat recoverable errors as fatal.
68 $recoverable = $error_level == E_RECOVERABLE_ERROR;
69 // As __toString() methods must not throw exceptions (recoverable errors)
70 // in PHP, we allow them to trigger a fatal error by emitting a user error
71 // using trigger_error().
72 $to_string = $error_level == E_USER_ERROR && substr($caller['function'], -strlen('__toString()')) == '__toString()';
74 '%type' => isset($types[$error_level]) ? $severity_msg : 'Unknown error',
75 // The standard PHP error handler considers that the error messages
76 // are HTML. We mimick this behavior here.
77 '@message' => Markup::create(Xss::filterAdmin($message)),
78 '%function' => $caller['function'],
79 '%file' => $caller['file'],
80 '%line' => $caller['line'],
81 'severity_level' => $severity_level,
82 'backtrace' => $backtrace,
83 '@backtrace_string' => (new \Exception())->getTraceAsString(),
84 ], $recoverable || $to_string);
86 // If the site is a test site then fail for user deprecations so they can be
87 // caught by the deprecation error handler.
88 elseif (DRUPAL_TEST_IN_CHILD_SITE && $error_level === E_USER_DEPRECATED) {
89 $backtrace = debug_backtrace();
90 $caller = Error::getLastCaller($backtrace);
92 Markup::create(Xss::filterAdmin($message)),
93 'User deprecated function',
102 * Determines whether an error should be displayed.
104 * When in maintenance mode or when error_level is ERROR_REPORTING_DISPLAY_ALL,
105 * all errors should be displayed. For ERROR_REPORTING_DISPLAY_SOME, $error
106 * will be examined to determine if it should be displayed.
109 * Optional error to examine for ERROR_REPORTING_DISPLAY_SOME.
112 * TRUE if an error should be displayed.
114 function error_displayable($error = NULL) {
115 if (defined('MAINTENANCE_MODE')) {
118 $error_level = _drupal_get_error_level();
119 if ($error_level == ERROR_REPORTING_DISPLAY_ALL || $error_level == ERROR_REPORTING_DISPLAY_VERBOSE) {
122 if ($error_level == ERROR_REPORTING_DISPLAY_SOME && isset($error)) {
123 return $error['%type'] != 'Notice' && $error['%type'] != 'Strict warning';
129 * Logs a PHP error or exception and displays an error page in fatal cases.
132 * An array with the following keys: %type, @message, %function, %file,
133 * %line, @backtrace_string, severity_level, and backtrace. All the parameters
134 * are plain-text, with the exception of @message, which needs to be an HTML
135 * string, and backtrace, which is a standard PHP backtrace.
138 * - An exception is thrown and not caught by something else.
139 * - A recoverable fatal error, which is a fatal error.
140 * Non-recoverable fatal errors cannot be logged by Drupal.
142 function _drupal_log_error($error, $fatal = FALSE) {
143 $is_installer = drupal_installation_attempted();
145 // Backtrace array is not a valid replacement value for t().
146 $backtrace = $error['backtrace'];
147 unset($error['backtrace']);
149 // When running inside the testing framework, we relay the errors
150 // to the tested site by the way of HTTP headers.
151 if (DRUPAL_TEST_IN_CHILD_SITE && !headers_sent() && (!defined('SIMPLETEST_COLLECT_ERRORS') || SIMPLETEST_COLLECT_ERRORS)) {
152 _drupal_error_header($error['@message'], $error['%type'], $error['%function'], $error['%file'], $error['%line']);
155 $response = new Response();
157 // Only call the logger if there is a logger factory available. This can occur
158 // if there is an error while rebuilding the container or during the
160 if (\Drupal::hasService('logger.factory')) {
162 // Provide the PHP backtrace to logger implementations.
163 \Drupal::logger('php')->log($error['severity_level'], '%type: @message in %function (line %line of %file) @backtrace_string.', $error + ['backtrace' => $backtrace]);
165 catch (\Exception $e) {
166 // We can't log, for example because the database connection is not
167 // available. At least try to log to PHP error log.
168 error_log(strtr('Failed to log error: %type: @message in %function (line %line of %file). @backtrace_string', $error));
172 // Log fatal errors, so developers can find and debug them.
174 error_log(sprintf('%s: %s in %s on line %d %s', $error['%type'], $error['@message'], $error['%file'], $error['%line'], $error['@backtrace_string']));
177 if (PHP_SAPI === 'cli') {
179 // When called from CLI, simply output a plain text message.
180 // Should not translate the string to avoid errors producing more errors.
181 $response->setContent(html_entity_decode(strip_tags(new FormattableMarkup('%type: @message in %function (line %line of %file).', $error))) . "\n");
187 if (\Drupal::hasRequest() && \Drupal::request()->isXmlHttpRequest()) {
189 if (error_displayable($error)) {
190 // When called from JavaScript, simply output the error message.
191 // Should not translate the string to avoid errors producing more errors.
192 $response->setContent(new FormattableMarkup('%type: @message in %function (line %line of %file).', $error));
199 // Display the message if the current error reporting level allows this type
200 // of message to be displayed, and unconditionally in update.php.
203 if (error_displayable($error)) {
206 // If error type is 'User notice' then treat it as debug information
207 // instead of an error message.
209 if ($error['%type'] == 'User notice') {
210 $error['%type'] = 'Debug';
214 // Attempt to reduce verbosity by removing DRUPAL_ROOT from the file path
215 // in the message. This does not happen for (false) security.
216 if (\Drupal::hasService('app.root')) {
217 $root_length = strlen(\Drupal::root());
218 if (substr($error['%file'], 0, $root_length) == \Drupal::root()) {
219 $error['%file'] = substr($error['%file'], $root_length + 1);
223 // Check if verbose error reporting is on.
224 $error_level = _drupal_get_error_level();
226 if ($error_level != ERROR_REPORTING_DISPLAY_VERBOSE) {
227 // Without verbose logging, use a simple message.
229 // We use \Drupal\Component\Render\FormattableMarkup directly here,
230 // rather than use t() since we are in the middle of error handling, and
231 // we don't want t() to cause further errors.
232 $message = new FormattableMarkup('%type: @message in %function (line %line of %file).', $error);
235 // With verbose logging, we will also include a backtrace.
237 // First trace is the error itself, already contained in the message.
238 // While the second trace is the error source and also contained in the
239 // message, the message doesn't contain argument values, so we output it
240 // once more in the backtrace.
241 array_shift($backtrace);
242 // Generate a backtrace containing only scalar argument values.
243 $error['@backtrace'] = Error::formatBacktrace($backtrace);
244 $message = new FormattableMarkup('%type: @message in %function (line %line of %file). <pre class="backtrace">@backtrace</pre>', $error);
249 // We fallback to a maintenance page at this point, because the page generation
250 // itself can generate errors.
251 // Should not translate the string to avoid errors producing more errors.
252 $message = 'The website encountered an unexpected error. Please try again later.' . '<br />' . $message;
255 // install_display_output() prints the output and ends script execution.
258 '#markup' => $message,
260 install_display_output($output, $GLOBALS['install_state'], $response->headers->all());
264 $response->setContent($message);
265 $response->setStatusCode(500, '500 Service unavailable (with message)');
268 // An exception must halt script execution.
273 if (\Drupal::hasService('session')) {
274 // Message display is dependent on sessions being available.
275 \Drupal::messenger()->addMessage($message, $class, TRUE);
285 * Returns the current error level.
287 * This function should only be used to get the current error level prior to the
288 * kernel being booted or before Drupal is installed. In all other situations
289 * the following code is preferred:
291 * \Drupal::config('system.logging')->get('error_level');
295 * The current error level.
297 function _drupal_get_error_level() {
298 // Raise the error level to maximum for the installer, so users are able to
299 // file proper bug reports for installer errors. The returned value is
300 // different to the one below, because the installer actually has a
301 // 'config.factory' service, which reads the default 'error_level' value from
302 // System module's default configuration and the default value is not verbose.
303 // @see error_displayable()
304 if (drupal_installation_attempted()) {
305 return ERROR_REPORTING_DISPLAY_VERBOSE;
308 // Try to get the error level configuration from database. If this fails,
309 // for example if the database connection is not there, try to read it from
312 $error_level = \Drupal::config('system.logging')->get('error_level');
314 catch (\Exception $e) {
315 $error_level = isset($GLOBALS['config']['system.logging']['error_level']) ? $GLOBALS['config']['system.logging']['error_level'] : ERROR_REPORTING_HIDE;
318 // If there is no container or if it has no config.factory service, we are
319 // possibly in an edge-case error situation while trying to serve a regular
320 // request on a public site, so use the non-verbose default value.
321 return $error_level ?: ERROR_REPORTING_DISPLAY_ALL;
325 * Adds error information to headers so that tests can access it.
332 * The function that emitted the error.
334 * The file that emitted the error.
336 * The line number in file that emitted the error.
338 function _drupal_error_header($message, $type, $function, $file, $line) {
339 // $number does not use drupal_static as it should not be reset
340 // as it uniquely identifies each PHP error.
346 'function' => $function,
351 // For non-fatal errors (e.g. PHP notices) _drupal_log_error can be called
352 // multiple times per request. In that case the response is typically
353 // generated outside of the error handler, e.g., in a controller. As a
354 // result it is not possible to use a Response object here but instead the
355 // headers need to be emitted directly.
356 header('X-Drupal-Assertion-' . $number . ': ' . rawurlencode(serialize($assertion)));