From 268c1cf327969bdbd8b269e96dcec0edf9c5f2a7 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Tue, 12 Nov 2019 11:04:42 +0100
Subject: [PATCH] Bug 23846: Display degraded view when MARCXML is invalid
 (staff detail)

When an invalid bibliographic record is imported into the catalogue
there is not warning or error. However the bibliographic record detail
page will explode (Koha::Biblio::Metadata->record will raise an
exception).

This patch proposes to catch the exception on this view and display a
warning about the situation.
Note that editing/saving the record will fix the MARCXML data and so
removes the warning (some black magic we should get rid of I suspect).

Test plan:
- Import a bibliographic record with invalid XML, you can add non
printable characters, like 0x1F (CTRL-V 1F with vim)
- Go to the detail page
=> Without this patch you get a 500
=> With this patch applied you get a "degraded view" with a warning
message, telling you what the error is.

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
---
 catalogue/detail.pl                                      | 9 ++++++++-
 .../intranet-tmpl/prog/en/modules/catalogue/detail.tt    | 6 ++++++
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/catalogue/detail.pl b/catalogue/detail.pl
index 3e2ca01cf5..511855e9fc 100755
--- a/catalogue/detail.pl
+++ b/catalogue/detail.pl
@@ -112,6 +112,9 @@ if ( not defined $record ) {
     exit;
 }
 
+eval { $biblio->metadata->record };
+$template->param( decoding_error => $@ );
+
 if($query->cookie("holdfor")){ 
     my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") );
     $template->param(
@@ -143,7 +146,11 @@ if ( $xslfile ) {
 }
 
 $template->param( 'SpineLabelShowPrintOnBibDetails' => C4::Context->preference("SpineLabelShowPrintOnBibDetails") );
-$template->param( ocoins => $biblio->get_coins );
+
+# Catch the exception as Koha::Biblio::Metadata->record can explode if the MARCXML is invalid
+# Do not propagate it as we already deal with it previously in this script
+my $coins = eval { $biblio->get_coins };
+$template->param( ocoins => $coins );
 
 # some useful variables for enhanced content;
 # in each case, we're grabbing the first value we find in
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt
index d7fc0ec3f2..5da7b95c18 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt
@@ -68,6 +68,12 @@
 [% CoceHost        = Koha.Preference('CoceHost') %]
 
 [% INCLUDE 'cat-toolbar.inc' %]
+    [% IF decoding_error %]
+        <div>
+            There is an error with this bibliographic record, the view may be degraded.
+            <br/> Error: [% decoding_error %]
+        </div>
+    [% END %]
     [% IF ( ocoins ) %]
         <!-- COinS / OpenURL -->
         <span class="Z3988" title="[% ocoins | html %]"></span>
-- 
2.24.0