Commit 40471b1d authored by Lorenz Struyf's avatar Lorenz Struyf Committed by Robin De Herdt
Browse files

Issue #3319682: Module fails on rendering on ES error response

parent 7608aa1b
Loading
Loading
Loading
Loading
+5 −0
Changes for elasticsearch_search_api.module: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -39,6 +39,11 @@ function elasticsearch_search_api_theme() {
        'did_you_mean' => NULL,
      ],
    ],
    'elasticsearch_search_api_error' => [
      'variables' => [
        'error_message' => NULL,
      ],
    ],
    'elasticsearch_search_api_facets_item_list' => [
      'variables' => [
        'items' => [],
+63 −17
Changes for src/Controller/SearchController.php: 63 added lines, 17 removed lines.
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\Core\Url;
use Elasticsearch\Common\Exceptions\ElasticsearchException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
@@ -185,16 +186,6 @@ class SearchController extends ControllerBase {
      throw new AccessDeniedHttpException();
    }

    $result = $this->parsedResult($searchAction);
    $hits = $this->renderHits($searchAction, $result, $query);

    // Requested another page in the result set.
    if ($request->isXmlHttpRequest() && !((bool) $request->get('ajax_form'))) {
      return $hits + ['#type' => 'container'];
    }

    $facets = $this->renderFacets($searchAction, $result);

    $drupalSettings = [
      'elasticsearch_search_api' => [
        'ajaxify' => [
@@ -205,13 +196,13 @@ class SearchController extends ControllerBase {
      ],
    ];

    return [
    $render_base = [
      '#theme' => 'elasticsearch_search_api_search',
      '#header' => $this->getSearchHeader(),
      '#facets' => $facets,
      '#results' => $hits,
      '#result_count' => $this->getResultCount($result),
      '#did_you_mean' => $this->getSuggestions($query),
      '#facets' => [],
      '#results' => [],
      '#result_count' => NULL,
      '#did_you_mean' => NULL,
      '#cache' => [
        'tags' => [
          'esa.search',
@@ -225,6 +216,31 @@ class SearchController extends ControllerBase {
        'drupalSettings' => $drupalSettings,
      ],
    ];

    try {
      $result = $this->parsedResult($searchAction);
    }
    catch (ElasticsearchException $e) {
      $render_base['#results'] = $this->getErrorMessage();
      return $render_base;
    }

    $hits = $this->renderHits($searchAction, $result, $query);

    // Requested another page in the result set.
    if ($request->isXmlHttpRequest() && !((bool) $request->get('ajax_form'))) {
      return $hits + ['#type' => 'container'];
    }

    $render = [
      '#header' => $this->getSearchHeader(),
      '#facets' => $this->renderFacets($searchAction, $result),
      '#results' => $hits,
      '#result_count' => $this->getResultCount($result),
      '#did_you_mean' => $this->getSuggestions($query),
    ];

    return array_merge($render_base, $render);
  }

  /**
@@ -322,7 +338,20 @@ class SearchController extends ControllerBase {
      throw new AccessDeniedHttpException();
    }

    $response = new AjaxResponse();
    try {
      $searchResult = $this->parsedResult($searchAction);
    }
    catch (ElasticsearchException $e) {
      $error = $this->getErrorMessage();

      // Replace search hits with error.
      $response->addCommand(new RemoveCommand('.esa-results-wrapper .results-wrapper > *'));
      $response->addCommand(new AppendCommand('.esa-results-wrapper .results-wrapper', $this->renderer->render($error)));

      return $response;
    }

    $facets = $this->renderFacets($searchAction, $searchResult);
    $hits = $this->renderHits($searchAction, $searchResult, $query);

@@ -341,8 +370,6 @@ class SearchController extends ControllerBase {
      ],
    ];

    $response = new AjaxResponse();

    // Replace suggestions.
    $response->addCommand(new RemoveCommand('.esa-results-wrapper .did-you-mean'));
    $suggestions = $this->getSuggestions($query);
@@ -508,7 +535,13 @@ class SearchController extends ControllerBase {
      return [];
    }

    try {
      $suggestions = $this->suggester->suggest($keyword);
    }
    catch (ElasticsearchException $e) {
      return [];
    }

    if (empty($suggestions)) {
      return [];
    }
@@ -536,6 +569,19 @@ class SearchController extends ControllerBase {
    ];
  }

  /**
   * Get a message to show to the client in case of an exception.
   *
   * @return array
   *   Render array to print the error message.
   */
  protected function getErrorMessage() {
    return [
      '#theme' => 'elasticsearch_search_api_error',
      '#error_message' => $this->t("Search results could not be retrieved."),
    ];
  }

  /**
   * Get a header to show above the search.
   *
+3 −0
Changes for templates/elasticsearch-search-api-error.html.twig: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
<div class="esa-error">
  {{ error_message }}
</div>