123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- <?php
- use Drupal\Component\Render\FormattableMarkup;
- use Drupal\Component\Utility\Xss;
- use Drupal\Core\Installer\InstallerKernel;
- use Drupal\Core\Logger\RfcLogLevel;
- use Drupal\Core\Render\Markup;
- use Drupal\Core\Utility\Error;
- use Symfony\Component\HttpFoundation\Response;
- function drupal_error_levels() {
- $types = [
- E_ERROR => ['Error', RfcLogLevel::ERROR],
- E_WARNING => ['Warning', RfcLogLevel::WARNING],
- E_PARSE => ['Parse error', RfcLogLevel::ERROR],
- E_NOTICE => ['Notice', RfcLogLevel::NOTICE],
- E_CORE_ERROR => ['Core error', RfcLogLevel::ERROR],
- E_CORE_WARNING => ['Core warning', RfcLogLevel::WARNING],
- E_COMPILE_ERROR => ['Compile error', RfcLogLevel::ERROR],
- E_COMPILE_WARNING => ['Compile warning', RfcLogLevel::WARNING],
- E_USER_ERROR => ['User error', RfcLogLevel::ERROR],
- E_USER_WARNING => ['User warning', RfcLogLevel::WARNING],
- E_USER_NOTICE => ['User notice', RfcLogLevel::NOTICE],
- E_STRICT => ['Strict warning', RfcLogLevel::DEBUG],
- E_RECOVERABLE_ERROR => ['Recoverable fatal error', RfcLogLevel::ERROR],
- E_DEPRECATED => ['Deprecated function', RfcLogLevel::DEBUG],
- E_USER_DEPRECATED => ['User deprecated function', RfcLogLevel::DEBUG],
- ];
- return $types;
- }
- function _drupal_error_handler_real($error_level, $message, $filename, $line, $context) {
- if ($error_level & error_reporting()) {
- $types = drupal_error_levels();
- list($severity_msg, $severity_level) = $types[$error_level];
- $backtrace = debug_backtrace();
- $caller = Error::getLastCaller($backtrace);
-
- $recoverable = $error_level == E_RECOVERABLE_ERROR;
-
-
-
- $to_string = $error_level == E_USER_ERROR && substr($caller['function'], -strlen('__toString()')) == '__toString()';
- _drupal_log_error([
- '%type' => isset($types[$error_level]) ? $severity_msg : 'Unknown error',
-
-
- '@message' => Markup::create(Xss::filterAdmin($message)),
- '%function' => $caller['function'],
- '%file' => $caller['file'],
- '%line' => $caller['line'],
- 'severity_level' => $severity_level,
- 'backtrace' => $backtrace,
- '@backtrace_string' => (new \Exception())->getTraceAsString(),
- ], $recoverable || $to_string);
- }
-
-
- elseif (DRUPAL_TEST_IN_CHILD_SITE && $error_level === E_USER_DEPRECATED) {
- static $seen = [];
- if (array_search($message, $seen, TRUE) === FALSE) {
-
-
- $seen[] = $message;
- $backtrace = debug_backtrace();
- $caller = Error::getLastCaller($backtrace);
- _drupal_error_header(
- Markup::create(Xss::filterAdmin($message)),
- 'User deprecated function',
- $caller['function'],
- $caller['file'],
- $caller['line']
- );
- }
- }
- }
- function error_displayable($error = NULL) {
- if (defined('MAINTENANCE_MODE')) {
- return TRUE;
- }
- $error_level = _drupal_get_error_level();
- if ($error_level == ERROR_REPORTING_DISPLAY_ALL || $error_level == ERROR_REPORTING_DISPLAY_VERBOSE) {
- return TRUE;
- }
- if ($error_level == ERROR_REPORTING_DISPLAY_SOME && isset($error)) {
- return $error['%type'] != 'Notice' && $error['%type'] != 'Strict warning';
- }
- return FALSE;
- }
- function _drupal_log_error($error, $fatal = FALSE) {
- $is_installer = InstallerKernel::installationAttempted();
-
- $backtrace = $error['backtrace'];
- unset($error['backtrace']);
-
-
- if (DRUPAL_TEST_IN_CHILD_SITE && !headers_sent() && (!defined('SIMPLETEST_COLLECT_ERRORS') || SIMPLETEST_COLLECT_ERRORS)) {
- _drupal_error_header($error['@message'], $error['%type'], $error['%function'], $error['%file'], $error['%line']);
- }
- $response = new Response();
-
-
-
- if (\Drupal::hasService('logger.factory')) {
- try {
-
- \Drupal::logger('php')->log($error['severity_level'], '%type: @message in %function (line %line of %file) @backtrace_string.', $error + ['backtrace' => $backtrace]);
- }
- catch (\Exception $e) {
-
-
- error_log(strtr('Failed to log error: %type: @message in %function (line %line of %file). @backtrace_string', $error));
- }
- }
-
- if ($fatal) {
- error_log(sprintf('%s: %s in %s on line %d %s', $error['%type'], $error['@message'], $error['%file'], $error['%line'], $error['@backtrace_string']));
- }
- if (PHP_SAPI === 'cli') {
- if ($fatal) {
-
-
- $response->setContent(html_entity_decode(strip_tags(new FormattableMarkup('%type: @message in %function (line %line of %file).', $error))) . "\n");
- $response->send();
- exit(1);
- }
- }
- if (\Drupal::hasRequest() && \Drupal::request()->isXmlHttpRequest()) {
- if ($fatal) {
- if (error_displayable($error)) {
-
-
- $response->setContent(new FormattableMarkup('%type: @message in %function (line %line of %file).', $error));
- $response->send();
- }
- exit;
- }
- }
- else {
-
-
- $message = '';
- $class = NULL;
- if (error_displayable($error)) {
- $class = 'error';
-
-
-
- if ($error['%type'] == 'User notice') {
- $error['%type'] = 'Debug';
- $class = 'status';
- }
-
-
- if (\Drupal::hasService('app.root')) {
- $root_length = strlen(\Drupal::root());
- if (substr($error['%file'], 0, $root_length) == \Drupal::root()) {
- $error['%file'] = substr($error['%file'], $root_length + 1);
- }
- }
-
- $error_level = _drupal_get_error_level();
- if ($error_level != ERROR_REPORTING_DISPLAY_VERBOSE) {
-
-
-
-
- $message = new FormattableMarkup('%type: @message in %function (line %line of %file).', $error);
- }
- else {
-
-
-
-
-
- array_shift($backtrace);
-
- $error['@backtrace'] = Error::formatBacktrace($backtrace);
- $message = new FormattableMarkup('%type: @message in %function (line %line of %file). <pre class="backtrace">@backtrace</pre>', $error);
- }
- }
- if ($fatal) {
-
-
-
- $message = 'The website encountered an unexpected error. Please try again later.' . '<br />' . $message;
- if ($is_installer) {
-
- $output = [
- '#title' => 'Error',
- '#markup' => $message,
- ];
- install_display_output($output, $GLOBALS['install_state'], $response->headers->all());
- exit;
- }
- $response->setContent($message);
- $response->setStatusCode(500, '500 Service unavailable (with message)');
- $response->send();
-
- exit;
- }
- if ($message) {
- if (\Drupal::hasService('session')) {
-
- \Drupal::messenger()->addMessage($message, $class, TRUE);
- }
- else {
- print $message;
- }
- }
- }
- }
- function _drupal_get_error_level() {
-
-
-
-
-
-
- if (InstallerKernel::installationAttempted()) {
- return ERROR_REPORTING_DISPLAY_VERBOSE;
- }
- $error_level = NULL;
-
-
-
- try {
- $error_level = \Drupal::config('system.logging')->get('error_level');
- }
- catch (\Exception $e) {
- $error_level = isset($GLOBALS['config']['system.logging']['error_level']) ? $GLOBALS['config']['system.logging']['error_level'] : ERROR_REPORTING_HIDE;
- }
-
-
-
- return $error_level ?: ERROR_REPORTING_DISPLAY_ALL;
- }
- function _drupal_error_header($message, $type, $function, $file, $line) {
-
-
- static $number = 0;
- $assertion = [
- $message,
- $type,
- [
- 'function' => $function,
- 'file' => $file,
- 'line' => $line,
- ],
- ];
-
-
-
-
-
- header('X-Drupal-Assertion-' . $number . ': ' . rawurlencode(serialize($assertion)));
- $number++;
- }
|