You are here

public function Debugger::logFailedRequest in Acquia Purge 8

Log the given failure with as much info as possible.

Parameters

\Exception $exception: The exception thrown in the request execution code path.

Overrides DebuggerInterface::logFailedRequest

File

src/Plugin/Purge/Purger/Debugger.php, line 179

Class

Debugger
Provides a centralized debugger for Acquia purger plugins.

Namespace

Drupal\acquia_purge\Plugin\Purge\Purger

Code

public function logFailedRequest(\Exception $exception) {
  $debug = [];
  $vars = [
    '@excmsg' => $exception
      ->getMessage(),
  ];

  // ConnectException's are frequent, rewrite its message somewhat.
  if ($exception instanceof ConnectException) {
    $vars['@excmsg'] = str_replace('cURL error 6: ', '', $vars['@excmsg']);
    $vars['@excmsg'] = str_replace('(see http://curl.haxx.se/libcurl/c/libcurl-errors.html)', '(this is allowed to happen incidentally when servers are slow, not structurally)', $vars['@excmsg']);
  }

  // Enrich debugging data with the request and response, if available.
  if ($exception instanceof RequestException) {
    $req = $exception
      ->getRequest();
    foreach ($this
      ->extractRequestInfo($req, TRUE) as $key => $value) {
      $debug[$key] = $value;
    }
    if ($exception
      ->hasResponse() && ($rsp = $exception
      ->getResponse())) {
      foreach ($this
        ->extractResponseInfo($rsp, TRUE) as $key => $value) {
        $debug[$key] = $value;
      }
    }
  }

  // Add exception and backtrace data.
  $debug = array_merge([
    'EXC' => $this
      ->extractClassName($exception),
  ], $debug);
  $debug['BACKTRACE'] = [];
  $path_strip = getcwd() . DIRECTORY_SEPARATOR;
  foreach (explode("\n", $exception
    ->getTraceAsString()) as $i => $frame) {
    if (in_array($i, [
      0,
      1,
      2,
    ])) {
      $debug['BACKTRACE'][$i] = str_replace($path_strip, '', $frame);
    }
  }

  // Log the normal message to the emergency output stream.
  $vars['%debug'] = json_encode($debug, JSON_PRETTY_PRINT);
  $this
    ->logger()
    ->error("@excmsg\n\n%debug", $vars);
}