Koha/Koha/Biblio/Metadata.pm
Fridolin Somers 3b987412f6 Bug 26171: Show biblionumber in Koha::Exceptions::Metadata::Invalid
Bug 23846 added a check for invalid MARCXML in search_for_data_inconsistencies.pl.
But the error message does not show the biblionumber of invalid biblio record.

Test plan:
1) Import a bibliographic record with invalid XML, you can add non printable characters, like 0x1F (CTRL-V 1F with vim)
2) Run misc/maintenance/search_for_data_inconsistencies.pl
3) Check you see correct biblionumber in error message :
Invalid data, cannot decode object (id=xxx, biblionumber=yyy, format=marcxml, schema=zzz, decoding_error=...

Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2021-01-04 13:29:50 +01:00

101 lines
2.3 KiB
Perl

package Koha::Biblio::Metadata;
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use MARC::Record;
use MARC::File::XML;
use Koha::Database;
use Koha::Exceptions::Metadata;
use base qw(Koha::Object);
=head1 NAME
Koha::Metadata - Koha Metadata Object class
=head1 API
=head2 Class methods
=cut
=head3 record
my $record = $metadata->record;
Returns an object representing the metadata record. The expected record type
corresponds to this table:
-------------------------------
| format | object type |
-------------------------------
| marcxml | MARC::Record |
-------------------------------
=head4 Error handling
=over
=item If an unsupported format is found, it throws a I<Koha::Exceptions::Metadata> exception.
=item If it fails to create the record object, it throws a I<Koha::Exceptions::Metadata::Invalid> exception.
=back
=cut
sub record {
my ($self) = @_;
my $record;
if ( $self->format eq 'marcxml' ) {
$record = eval { MARC::Record::new_from_xml( $self->metadata, 'UTF-8', $self->schema ); };
my $marcxml_error = $@;
chomp $marcxml_error;
unless ($record) {
Koha::Exceptions::Metadata::Invalid->throw(
id => $self->id,
biblionumber => $self->biblionumber,
format => $self->format,
schema => $self->schema,
decoding_error => $marcxml_error,
);
}
}
else {
Koha::Exceptions::Metadata->throw(
'Koha::Biblio::Metadata->record called on unhandled format: ' . $self->format );
}
return $record;
}
=head2 Internal methods
=head3 _type
=cut
sub _type {
return 'BiblioMetadata';
}
1;