From 3fcc808c5b369d19009eb6ed0a5c2705722df01f Mon Sep 17 00:00:00 2001
From: Dorian Merz <merz@ub.uni-leipzig.de>
Date: Wed, 9 Jun 2021 14:40:47 +0200
Subject: [PATCH] refs #20029 [finc] add messages on pages for missing errors *
 more specific message for RecordMissing exception * redirect message in case
 a fallback is used

---
 local/languages/de.ini                        |  3 ++
 local/languages/en.ini                        |  3 ++
 .../src/finc/Controller/RecordController.php  | 23 ++++++----
 .../finc/templates/error/RecordMissing.phtml  | 46 +++++++++++++++++++
 4 files changed, 67 insertions(+), 8 deletions(-)
 create mode 100644 themes/finc/templates/error/RecordMissing.phtml

diff --git a/local/languages/de.ini b/local/languages/de.ini
index ce9f68c9a27..654d7be086d 100644
--- a/local/languages/de.ini
+++ b/local/languages/de.ini
@@ -2076,3 +2076,6 @@ select_item_storage_retrieval_request_cancel = "Titel auswählen, um Magazinbest
 
 ; #17915
 toggle-dropdown = "Untermenü aufklappen"
+
+missing_record_redirect = "Der aufgerufene Titel ist nicht vorhanden. Stattdessen wurde eine Suche ausgelöst"
+missing_record_exception = "Der aufgerufene Titel (%%id%%) ist nicht vorhanden."
diff --git a/local/languages/en.ini b/local/languages/en.ini
index 6e6fe795431..b8214638d28 100644
--- a/local/languages/en.ini
+++ b/local/languages/en.ini
@@ -2156,3 +2156,6 @@ Start a new Basic Search = "New Basic Search"
 
 ; #17915
 toggle-dropdown = "Toggle Dropdown"
+
+missing_record_redirect = "Record unavailable. You have been redirected to a search."
+missing_record_exception = "Record %%id%% is unavailable."
diff --git a/module/finc/src/finc/Controller/RecordController.php b/module/finc/src/finc/Controller/RecordController.php
index ee9b3d49e75..be0586f1205 100644
--- a/module/finc/src/finc/Controller/RecordController.php
+++ b/module/finc/src/finc/Controller/RecordController.php
@@ -30,6 +30,7 @@
 namespace finc\Controller;
 
 use finc\Rewrite\EblRewrite;
+use VuFind\Exception\RecordMissing;
 use Zend\Log\LoggerAwareInterface as LoggerAwareInterface;
 use Zend\View\View;
 
@@ -121,19 +122,25 @@ class RecordController extends \VuFind\Controller\RecordController implements
      */
     public function homeAction()
     {
-        if ($this->params()->fromQuery('catchMissing', null)) {
-            try {
-                return parent::homeAction();
-            } catch (\VuFind\Exception\RecordMissing $ex) {
-                $lookfor = $this->params()->fromQuery('catchMissing_lookfor','');
-                if (empty($lookfor)) throw $ex;
-                $type = $this->params()->fromQuery('catchMissing_searchType','Allfields');
+        try {
+            return parent::homeAction();
+        } catch (\VuFind\Exception\RecordMissing $ex) {
+            if (
+                $this->params()->fromQuery('catchMissing', null)
+                &&
+                $lookfor = $this->params()->fromQuery('catchMissing_lookfor', '')
+            ) {
+                $type = $this->params()->fromQuery('catchMissing_searchType', 'Allfields');
                 $link = $this->redirect()->toRoute(
                     'search-results',
                     [],
-                    ['query' => compact('lookfor','type')]
+                    ['query' => compact('lookfor', 'type')]
                 );
+                $this->flashMessenger()->addErrorMessage('missing_record_redirect');
                 return $link;
+            } else {
+                $id = $this->params()->fromRoute('id');
+                throw new RecordMissing($id);
             }
         }
 
diff --git a/themes/finc/templates/error/RecordMissing.phtml b/themes/finc/templates/error/RecordMissing.phtml
new file mode 100644
index 00000000000..6cc0d7358a9
--- /dev/null
+++ b/themes/finc/templates/error/RecordMissing.phtml
@@ -0,0 +1,46 @@
+<!-- finc: RuntimeException -->
+<?php /* largely copied from /bootstrap3/templates/error/index.phtml #17035 - RL */?>
+<?php
+  // Set page title.
+  $this->headTitle($this->translate('Missing Record'));
+
+  $this->layout()->breadcrumbs = '<li class="active">Error</li>';
+?>
+<div class="alert alert-danger">
+  <p><?=$this->transEsc('An error has occurred')?></p>
+      <p><?=$this->transEsc(
+              'missing_record_exception',
+              ['%%id%%' => $this->exception->getMessage()]
+              )
+        ?></p>
+</div>
+<form>
+    <input type="button" class="btn" value="<?=$this->translate('navigate_back')?>" onclick="history.back()">
+</form>
+
+<?php if (isset($this->display_exceptions) && $this->display_exceptions): ?>
+  <h2><?=$this->transEsc('Exception')?>:</h2>
+  <p>
+    <b><?=$this->transEsc('Message')?>:</b> <?=$this->escapeHtml($this->exception->getMessage())?>
+  </p>
+
+  <h2><?=$this->transEsc('Backtrace')?>:</h2>
+  <pre><?=$this->exception->getTraceAsString()?>
+  </pre>
+
+  <?php if ($e = $this->exception->getPrevious()): ?>
+    <h3>Previous exceptions:</h3>
+    <?php while ($e): ?>
+        <h4><?php echo get_class($e); ?></h4>
+        <p><?=$e->getMessage()?></p>
+        <pre><?=$e->getTraceAsString()?></pre>
+        <?php $e = $e->getPrevious(); ?>
+    <?php endwhile; ?>
+  <?php endif; ?>
+
+  <?php if (isset($this->request)): ?>
+    <h2><?=$this->transEsc('error_page_parameter_list_heading')?>:</h2>
+    <pre><?=$this->escapeHtml(var_export($this->request->getParams(), true))?></pre>
+  <?php endif; ?>
+<?php endif ?>
+<!-- finc: RuntimeException - END -->
-- 
GitLab