From 64eb3b02cd422d8b52b51d7c92b851e85f2cfe78 Mon Sep 17 00:00:00 2001
From: Ere Maijala <ere.maijala@helsinki.fi>
Date: Thu, 10 Oct 2019 19:32:03 +0300
Subject: [PATCH] Add support for reporting errors returned from a search
 backend. (#1455)

---
 languages/en.ini                              |  1 +
 languages/fi.ini                              |  1 +
 languages/sv.ini                              |  1 +
 .../src/VuFind/Controller/AbstractSearch.php  |  4 ++++
 .../VuFind/src/VuFind/Search/Base/Results.php | 21 +++++++++++++++++++
 .../src/VuFind/Search/Primo/Results.php       |  1 +
 .../VuFindSearch/Backend/Primo/Connector.php  |  2 +-
 .../Primo/Response/RecordCollection.php       | 10 +++++++++
 .../Response/AbstractRecordCollection.php     | 10 +++++++++
 .../Backend/Primo/ConnectorTest.php           |  2 +-
 10 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/languages/en.ini b/languages/en.ini
index 92d4692b660..f04255c7364 100644
--- a/languages/en.ini
+++ b/languages/en.ini
@@ -349,6 +349,7 @@ email_subject = "Subject"
 email_success = "Message Sent"
 Empty = "Empty"
 Empty Book Bag = "Empty Book Bag"
+empty_search_disallowed = "An empty query is not allowed with the current search target"
 Enable Auto Config = "Enable Auto Config"
 End Page = "End Page"
 Era = "Era"
diff --git a/languages/fi.ini b/languages/fi.ini
index cb3fb51815a..769729c0d75 100644
--- a/languages/fi.ini
+++ b/languages/fi.ini
@@ -347,6 +347,7 @@ email_subject = "Aihe"
 email_success = "Viesti lähetetty"
 Empty = "Tyhjä"
 Empty Book Bag = "Tyhjennä kirjakori"
+empty_search_disallowed = "Käytetystä hakukohteesta ei voi hakea ilman hakuehtoja"
 Enable Auto Config = "Ota käyttöön automaattinen asennus"
 End Page = "Viimeinen sivu"
 Era = "Aikakausi"
diff --git a/languages/sv.ini b/languages/sv.ini
index 586c1d57b3d..d32a7c0a82f 100644
--- a/languages/sv.ini
+++ b/languages/sv.ini
@@ -341,6 +341,7 @@ email_subject = "Ämne"
 email_success = "Meddelandet har skickats"
 Empty = "Tom"
 Empty Book Bag = "Tömma"
+empty_search_disallowed = "Det går inte att söka i nuvarande sökmål utan söktermer"
 Enable Auto Config = "Enable Auto Config"
 End Page = "Sista sidan"
 Era = "Tidsperiod"
diff --git a/module/VuFind/src/VuFind/Controller/AbstractSearch.php b/module/VuFind/src/VuFind/Controller/AbstractSearch.php
index 80792d8f4e1..f7714bc4f42 100644
--- a/module/VuFind/src/VuFind/Controller/AbstractSearch.php
+++ b/module/VuFind/src/VuFind/Controller/AbstractSearch.php
@@ -310,6 +310,10 @@ class AbstractSearch extends AbstractBase
             if ($this->resultScrollerActive()) {
                 $this->resultScroller()->init($results);
             }
+
+            foreach ($results->getErrors() as $error) {
+                $this->flashMessenger()->addErrorMessage($error);
+            }
         }
 
         // Special case: If we're in RSS view, we need to render differently:
diff --git a/module/VuFind/src/VuFind/Search/Base/Results.php b/module/VuFind/src/VuFind/Search/Base/Results.php
index 2dd1b20d980..6b99ea66048 100644
--- a/module/VuFind/src/VuFind/Search/Base/Results.php
+++ b/module/VuFind/src/VuFind/Search/Base/Results.php
@@ -74,6 +74,13 @@ abstract class Results
      */
     protected $results = null;
 
+    /**
+     * Any errors reported by the search backend
+     *
+     * @var array
+     */
+    protected $errors = null;
+
     /**
      * An ID number for saving/retrieving search
      *
@@ -257,6 +264,7 @@ abstract class Results
         $this->resultTotal = 0;
         $this->results = [];
         $this->suggestions = [];
+        $this->errors = [];
 
         // Run the search:
         $this->startQueryTimer();
@@ -375,6 +383,19 @@ abstract class Results
         return $this->results;
     }
 
+    /**
+     * Basic 'getter' for errors.
+     *
+     * @return array
+     */
+    public function getErrors()
+    {
+        if (null === $this->errors) {
+            $this->performAndProcessSearch();
+        }
+        return $this->errors;
+    }
+
     /**
      * Basic 'getter' for ID of saved search.
      *
diff --git a/module/VuFind/src/VuFind/Search/Primo/Results.php b/module/VuFind/src/VuFind/Search/Primo/Results.php
index 3ebb7b57244..f2fac9cc386 100644
--- a/module/VuFind/src/VuFind/Search/Primo/Results.php
+++ b/module/VuFind/src/VuFind/Search/Primo/Results.php
@@ -56,6 +56,7 @@ class Results extends \VuFind\Search\Base\Results
 
         $this->responseFacets = $collection->getFacets();
         $this->resultTotal = $collection->getTotal();
+        $this->errors = $collection->getErrors();
 
         // Construct record drivers for all the items in the response:
         $this->results = $collection->getRecords();
diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/Primo/Connector.php b/module/VuFindSearch/src/VuFindSearch/Backend/Primo/Connector.php
index fe85b838359..dacafdfa4a3 100644
--- a/module/VuFindSearch/src/VuFindSearch/Backend/Primo/Connector.php
+++ b/module/VuFindSearch/src/VuFindSearch/Backend/Primo/Connector.php
@@ -83,7 +83,7 @@ class Connector implements \Zend\Log\LoggerAwareInterface
         'recordCount' => 0,
         'documents' => [],
         'facets' => [],
-        'error' => 'Primo does not accept an empty query'
+        'error' => 'empty_search_disallowed'
     ];
 
     /**
diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/Primo/Response/RecordCollection.php b/module/VuFindSearch/src/VuFindSearch/Backend/Primo/Response/RecordCollection.php
index 58ca3626496..f5392e46277 100644
--- a/module/VuFindSearch/src/VuFindSearch/Backend/Primo/Response/RecordCollection.php
+++ b/module/VuFindSearch/src/VuFindSearch/Backend/Primo/Response/RecordCollection.php
@@ -93,4 +93,14 @@ class RecordCollection extends AbstractRecordCollection
         $size = $this->response['query']['pageSize'] ?? 0;
         return $page * $size;
     }
+
+    /**
+     * Return any errors.
+     *
+     * @return array
+     */
+    public function getErrors()
+    {
+        return (array)($this->response['error'] ?? []);
+    }
 }
diff --git a/module/VuFindSearch/src/VuFindSearch/Response/AbstractRecordCollection.php b/module/VuFindSearch/src/VuFindSearch/Response/AbstractRecordCollection.php
index 45b97353382..9320e7f226c 100644
--- a/module/VuFindSearch/src/VuFindSearch/Response/AbstractRecordCollection.php
+++ b/module/VuFindSearch/src/VuFindSearch/Response/AbstractRecordCollection.php
@@ -77,6 +77,16 @@ abstract class AbstractRecordCollection implements RecordCollectionInterface
         return $this->records;
     }
 
+    /**
+     * Return any errors.
+     *
+     * @return array
+     */
+    public function getErrors()
+    {
+        return [];
+    }
+
     /**
      * Shuffles records.
      *
diff --git a/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Primo/ConnectorTest.php b/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Primo/ConnectorTest.php
index 01af661861a..c2cd04ca7a3 100644
--- a/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Primo/ConnectorTest.php
+++ b/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Primo/ConnectorTest.php
@@ -81,7 +81,7 @@ class ConnectorTest extends TestCase
         $terms = [];
         $result = $conn->query('dummyinst', $terms);
         $this->assertEquals(0, $result['recordCount']);
-        $this->assertEquals('Primo does not accept an empty query', $result['error']);
+        $this->assertEquals('empty_search_disallowed', $result['error']);
     }
 
     /**
-- 
GitLab