summaryrefslogtreecommitdiff
path: root/lib/Travel/Status/DE/IRIS.pm
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2018-09-08 14:30:57 +0200
committerDaniel Friesel <derf@finalrewind.org>2018-09-08 14:30:57 +0200
commitf33f8a6553ae624b1e58c39e6d4cf5456ce48cf2 (patch)
treee91862b893eb133ab5c9ed89ab205cc75aff0196 /lib/Travel/Status/DE/IRIS.pm
parenta13eaa989f5d93b9b7816a9c819821d79f7eb54f (diff)
Handle _all_ cases of invalid XML
Diffstat (limited to 'lib/Travel/Status/DE/IRIS.pm')
-rw-r--r--lib/Travel/Status/DE/IRIS.pm34
1 files changed, 28 insertions, 6 deletions
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');