From 1a380d05a267e54a124af4d4da1b9b1eaafec50c Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 25 Feb 2021 15:21:17 +0000 Subject: [PATCH] Bug 27806: (follow-up) Add Koha::Exception for bad queries This patch adds a PropertyNotFound exception tothe 'as_list' method to catch DBIx::Class excpetions caused by passing bad column names into a query. --- Koha/Exceptions/Object.pm | 4 ++++ Koha/Objects.pm | 29 +++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/Koha/Exceptions/Object.pm b/Koha/Exceptions/Object.pm index 688a97cd5a..0e0742e655 100644 --- a/Koha/Exceptions/Object.pm +++ b/Koha/Exceptions/Object.pm @@ -40,6 +40,7 @@ use Exception::Class ( 'Koha::Exceptions::Object::PropertyNotFound' => { isa => 'Koha::Exceptions::Object', description => "Invalid property", + fields => ['property', 'clause'], }, 'Koha::Exceptions::Object::ReadOnlyProperty' => { isa => 'Koha::Exceptions::Object', @@ -84,6 +85,9 @@ sub full_message { elsif ( $self->isa('Koha::Exceptions::Object::NotInstantiated') ) { $msg = sprintf("Tried to access the '%s' method, but %s is not instantiated", $self->method, $self->class ); } + elsif ( $self->isa('Koha::Exceptions::Object::PropertyNotFound') ) { + $msg = sprintf("Invalid property '%s' in '%s' clause", $self->property, $self->clause ); + } } return $msg; diff --git a/Koha/Objects.pm b/Koha/Objects.pm index 2f16b35888..06f3a18f64 100644 --- a/Koha/Objects.pm +++ b/Koha/Objects.pm @@ -22,6 +22,7 @@ use Modern::Perl; use Carp; use List::MoreUtils qw( none ); use Class::Inspector; +use Try::Tiny; use Koha::Database; use Koha::Exceptions::Object; @@ -409,9 +410,33 @@ Returns an arrayref of the objects in this set. =cut sub as_list { - my ( $self ) = @_; + my ($self) = @_; - my @dbic_rows = $self->_resultset()->all(); + my @dbic_rows = try { + $self->_resultset()->all(); + } + catch { + if ( ref($_) eq 'DBIx::Class::Exception' ) { + #warn $_->{msg}; + if ( $_->{msg} =~ + /Unknown column '(?.*?)' in 'where clause'/ ) + { + Koha::Exceptions::Object::PropertyNotFound->throw( + property => $+{column}, + clause => 'where' + ); + } + elsif ( $_->{msg} =~ + /Unknown column '(?.*?)' in 'order clause'/ ) + { + Koha::Exceptions::Object::PropertyNotFound->throw( + property => $+{column}, + clause => 'order' + ); + } + } + $_->rethrow(); + }; my @objects = $self->_wrap(@dbic_rows); -- 2.20.1