From d71b923ea0f7d16b19bb7e98176609726867b98a Mon Sep 17 00:00:00 2001
From: Dan Scott <dscott@laurentian.ca>
Date: Fri, 25 Nov 2011 12:30:24 -0500
Subject: [PATCH] TPAC: Protect against missing 901c in legacy MARCXML

Bad MARCXML to which Evergreen has been unable to add a 901c happens -
particularly with legacy MARCXML. Currently, TPAC crashes hard with an
ugly error if a 901c is not found when attempting to display the record
in search results. In comparison, the JSPAC displays the record without
a problem.

This commit logs a warning and enables the TPAC to continue somewhat
gracefully (the problem record is simply skipped in the search results).
A more robust solution may be to teach unapi.bre to add the record ID to
the results and to have the TPAC pull from that, rather than relying on
the MARCXML itself to contain the record ID.

Signed-off-by: Dan Scott <dscott@laurentian.ca>
Signed-off-by: Bill Erickson <berick@esilibrary.com>
---
 Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Util.pm | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Util.pm b/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Util.pm
index af027e3c6c..1199d9ae38 100644
--- a/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Util.pm
+++ b/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Util.pm
@@ -180,7 +180,15 @@ sub get_records_and_facets {
             my($self, $req) = @_;
             my $data = $req->{response}->[0]->content;
             my $xml = XML::LibXML->new->parse_string($data->{'unapi.bre'})->documentElement;
-            my $bre_id =  $xml->find('*[@tag="901"]/*[@code="c"]')->[0]->textContent;
+
+            # Protect against legacy invalid MARCXML that might not have a 901c
+            my $bre_id;
+            my $bre_id_nodes =  $xml->find('*[@tag="901"]/*[@code="c"]');
+            if ($bre_id_nodes) {
+                $bre_id =  $bre_id_nodes->[0]->textContent;
+            } else {
+                $logger->warn("Missing 901 subfield 'c' in " . $xml->toString());
+            }
             push(@data, {id => $bre_id, marc_xml => $xml});
         }
     );
-- 
2.11.0