From 4e5d250b7304994b98da342717c1c7acbea250d4 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sat, 21 Nov 2020 10:44:27 +0100 Subject: Do not croak when receiving invalid XML --- lib/Travel/Status/DE/HAFAS.pm | 12 +++++++++--- t/30-invalid-xml.t | 17 ++++++++++++++--- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/Travel/Status/DE/HAFAS.pm b/lib/Travel/Status/DE/HAFAS.pm index 670cba6..839a599 100644 --- a/lib/Travel/Status/DE/HAFAS.pm +++ b/lib/Travel/Status/DE/HAFAS.pm @@ -180,9 +180,15 @@ sub new { # errors in delay="...") when setting the language to dutch/italian. # No, I don't know why. - $ref->{tree} = XML::LibXML->load_xml( - string => $ref->{raw_xml}, - ); + eval { $ref->{tree} = XML::LibXML->load_xml( string => $ref->{raw_xml} ) }; + + if ( my $err = $@ ) { + if ( $ref->{developer_mode} ) { + say $ref->{raw_xml}; + } + $ref->{errstr} = "Backend returned invalid XML: $err"; + return $ref; + } if ( $ref->{developer_mode} ) { say $ref->{tree}->toString(1); diff --git a/t/30-invalid-xml.t b/t/30-invalid-xml.t index cb83a4c..4a358a2 100755 --- a/t/30-invalid-xml.t +++ b/t/30-invalid-xml.t @@ -6,7 +6,7 @@ use 5.020; use utf8; use File::Slurp qw(read_file); -use Test::More tests => 1; +use Test::More tests => 2; use Travel::Status::DE::HAFAS; @@ -15,7 +15,18 @@ my $xml = 'lol'; my $status = Travel::Status::DE::HAFAS->new( service => 'DB', station => 'Berlin Jannowitzbrücke', - xml => $xml + xml => $xml ); -is (scalar $status->results, 0, 'no results on invalid input'); +is( scalar $status->results, + 0, 'no results on valid XML with invalid HAFAS data' ); + +$xml = 'lol<'; + +$status = Travel::Status::DE::HAFAS->new( + service => 'DB', + station => 'Berlin Jannowitzbrücke', + xml => $xml +); + +is( scalar $status->results, 0, 'no results on invalid XML' ); -- cgit v1.2.3