From 7bc6240eeb0672f52e5f1ab4c20fe4e60bc22e6e Mon Sep 17 00:00:00 2001
From: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Date: Mon, 4 Nov 2024 09:23:47 +0000
Subject: [PATCH] Bug 30657: Try parent method first

This patch adds a try/catch inside the Koha::Patron AUTOLOAD to try and
find the method on the parent, including it's AUTOLOAD, prior to running
the local AUTOLOAD code to generate attribute accessors and then use the
called method.
---
 Koha/Patron.pm | 76 +++++++++++++++++++++++++++-----------------------
 1 file changed, 41 insertions(+), 35 deletions(-)

diff --git a/Koha/Patron.pm b/Koha/Patron.pm
index 3e56e8911da..26b34c3974f 100644
--- a/Koha/Patron.pm
+++ b/Koha/Patron.pm
@@ -130,44 +130,50 @@ sub AUTOLOAD {
     my $self = shift(@args);
     my $name = $AUTOLOAD;
     $name =~ s/.*:://;    # Remove package name
-    my $found = 0;
-
-    # Fetch attributes dynamically
-    my $attribute_types = Koha::Patron::Attribute::Types->search();
-    while ( my $attr = $attribute_types->next ) {
-        my $code = $attr->code;
-        $found = 1 if ( $code eq $name );
-
-        # Dynamically create accessor method
-        no strict 'refs';
-        if ( !defined *{ ref($self) . "::$code" }{CODE} ) {
-            if ( $attr->repeatable ) {
-                *{ ref($self) . "::$code" } = sub {
-                    my ($self) = @_;
-                    return $self->_result->get_column($code) if $self->_result->has_column($code);
-                    my $attributes = $self->_result->borrower_attributes->search( { code => $code } );
-                    my $values     = [];
-                    while ( my $attribute = $attributes->next ) {
-                        push @$values, $attribute->attribute;
-                    }
-                    return $values;
-                };
-            } else {
-                *{ ref($self) . "::$code" } = sub {
-                    my ($self) = @_;
-                    return $self->_result->get_column($code) if $self->_result->has_column($code);
-                    my $attribute = $self->_result->borrower_attributes->search( { code => $code } )->first;
-                    return $attribute ? $attribute->attribute : undef;
-                };
+
+    return try {
+        my $wt = 'SUPER::' . $name;
+        $self->$wt(@args);
+    } catch {
+        my $error = $_;
+
+        # Fetch attributes dynamically
+        my $found           = 0;
+        my $attribute_types = Koha::Patron::Attribute::Types->search();
+        while ( my $attr = $attribute_types->next ) {
+            my $code = $attr->code;
+            $found = 1 if ( $code eq $name );
+
+            # Dynamically create accessor method
+            no strict 'refs';
+            if ( !defined *{ ref($self) . "::$code" }{CODE} ) {
+                if ( $attr->repeatable ) {
+                    *{ ref($self) . "::$code" } = sub {
+                        my ($self) = @_;
+                        return $self->_result->get_column($code) if $self->_result->has_column($code);
+                        my $attributes = $self->_result->borrower_attributes->search( { code => $code } );
+                        my $values     = [];
+                        while ( my $attribute = $attributes->next ) {
+                            push @$values, $attribute->attribute;
+                        }
+                        return $values;
+                    };
+                } else {
+                    *{ ref($self) . "::$code" } = sub {
+                        my ($self) = @_;
+                        return $self->_result->get_column($code) if $self->_result->has_column($code);
+                        my $attribute = $self->_result->borrower_attributes->search( { code => $code } )->first;
+                        return $attribute ? $attribute->attribute : undef;
+                    };
+                }
             }
         }
-    }
 
-    if ($found) {
-        return $self->$name();
-    } else {
-        my $wt = 'SUPER::' . $name;
-        return $self->$wt(@args);
+        if ($found) {
+            $self->$name();
+        } else {
+            $error->rethrow;
+        }
     }
 }
 
-- 
2.47.0