From f33f8a6553ae624b1e58c39e6d4cf5456ce48cf2 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sat, 8 Sep 2018 14:30:57 +0200 Subject: Handle _all_ cases of invalid XML --- lib/Travel/Status/DE/IRIS.pm | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) (limited to 'lib/Travel') diff --git a/lib/Travel/Status/DE/IRIS.pm b/lib/Travel/Status/DE/IRIS.pm index efb080d..e6dcda3 100644 --- a/lib/Travel/Status/DE/IRIS.pm +++ b/lib/Travel/Status/DE/IRIS.pm @@ -18,6 +18,19 @@ use LWP::UserAgent; use Travel::Status::DE::IRIS::Result; use XML::LibXML; +sub try_load_xml { + my ($xml) = @_; + + my $tree; + + eval { $tree = XML::LibXML->load_xml( string => $xml ) }; + + if ($@) { + return ( undef, $@ ); + } + return ( $tree, undef ); +} + sub new { my ( $class, %opt ) = @_; @@ -215,10 +228,9 @@ sub get_station { } } - my $xml_st; - eval { $xml_st = XML::LibXML->load_xml( string => $raw ) }; - if ($@) { - $self->{errstr} = "Parse error: $@"; + my ( $xml_st, $xml_err ) = try_load_xml($raw); + if ($xml_err) { + $self->{errstr} = 'Failed to parse station data: Invalid XML'; return; } @@ -353,7 +365,12 @@ sub get_timetable { return $self; } - my $xml = XML::LibXML->load_xml( string => $raw ); + my ( $xml, $xml_err ) = try_load_xml($raw); + + if ($xml_err) { + $self->{warnstr} = 'Failed to parse a schedule part: Invalid XML'; + return $self; + } my $station = ( $xml->findnodes('/timetable') )[0]->getAttribute('station'); @@ -379,7 +396,12 @@ sub get_realtime { return $self; } - my $xml = XML::LibXML->load_xml( string => $raw ); + my ( $xml, $xml_err ) = try_load_xml($raw); + + if ($xml_err) { + $self->{warnstr} = 'Failed to parse realtime data: Invalid XML'; + return $self; + } my $station = ( $xml->findnodes('/timetable') )[0]->getAttribute('station'); -- cgit v1.2.3