You are here

protected function RemotePostWebformHandler::handleError in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformHandler/RemotePostWebformHandler.php \Drupal\webform\Plugin\WebformHandler\RemotePostWebformHandler::handleError()

Handle error by logging and display debugging and/or exception message.

Parameters

string $state: The state of the webform submission. Either STATE_NEW, STATE_DRAFT_CREATED, STATE_DRAFT_UPDATED, STATE_COMPLETED, STATE_UPDATED, or STATE_CONVERTED depending on the last save operation performed.

string $message: Message to be displayed.

string $request_url: The remote URL the request is being posted to.

string $request_method: The method of remote post.

string $request_type: The type of remote post.

string $request_options: The requests options including the submission data.

\Psr\Http\Message\ResponseInterface|null $response: The response returned by the remote server.

1 call to RemotePostWebformHandler::handleError()
RemotePostWebformHandler::remotePost in src/Plugin/WebformHandler/RemotePostWebformHandler.php
Execute a remote post.

File

src/Plugin/WebformHandler/RemotePostWebformHandler.php, line 1032

Class

RemotePostWebformHandler
Webform submission remote post handler.

Namespace

Drupal\webform\Plugin\WebformHandler

Code

protected function handleError($state, $message, $request_url, $request_method, $request_type, $request_options, $response) {
  global $base_url, $base_path;

  // If debugging is enabled, display the error message on screen.
  $this
    ->debug($message, $state, $request_url, $request_method, $request_type, $request_options, $response, 'error');

  // Log error message.
  $context = [
    '@form' => $this
      ->getWebform()
      ->label(),
    '@state' => $state,
    '@type' => $request_type,
    '@url' => $request_url,
    '@message' => $message,
    'webform_submission' => $this
      ->getWebformSubmission(),
    'handler_id' => $this
      ->getHandlerId(),
    'operation' => 'error',
    'link' => $this
      ->getWebform()
      ->toLink($this
      ->t('Edit'), 'handlers')
      ->toString(),
  ];
  $this
    ->getLogger('webform_submission')
    ->error('@form webform remote @type post (@state) to @url failed. @message', $context);

  // Display custom or default exception message.
  if (!$this
    ->displayCustomResponseMessage($response, TRUE)) {
    $this->messageManager
      ->display(WebformMessageManagerInterface::SUBMISSION_EXCEPTION_MESSAGE, 'error');
  }

  // Redirect the current request to the error url.
  $error_url = $this->configuration['error_url'];
  if ($error_url && PHP_SAPI !== 'cli') {

    // Convert error path to URL.
    if (strpos($error_url, '/') === 0) {
      $error_url = $base_url . preg_replace('#^' . $base_path . '#', '/', $error_url);
    }
    $request = $this->requestStack
      ->getCurrentRequest();

    // Build Ajax redirect or trusted redirect response.
    $wrapper_format = $request
      ->get(MainContentViewSubscriber::WRAPPER_FORMAT);
    $is_ajax_request = $wrapper_format === 'drupal_ajax';
    if ($is_ajax_request) {
      $response = new AjaxResponse();
      $response
        ->addCommand(new RedirectCommand($error_url));
      $response
        ->setData($response
        ->getCommands());
    }
    else {
      $response = new TrustedRedirectResponse($error_url);
    }

    // Save the session so things like messages get saved.
    $request
      ->getSession()
      ->save();
    $response
      ->prepare($request);

    // Make sure to trigger kernel events.
    $this->kernel
      ->terminate($request, $response);
    $response
      ->send();

    // Only exit, an Ajax request to prevent headers from being overwritten.
    if ($is_ajax_request) {
      exit;
    }
  }
}