From b686560537b3e522fdfd0f8a81fa1d9b7c7114d8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Fri, 15 Apr 2016 19:18:58 +0200 Subject: [PATCH] Menu: Fehlerchecks beim yaml einlesen MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 2 häufige Fehler abfangen: - wenn ids in einer datei doppelt vorkommen (passiert beim editieren) - wenn YAML selber Fehler wirft gab es bisher ein HTTP 500 --- SL/Menu.pm | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/SL/Menu.pm b/SL/Menu.pm index 6bbcffb9c..bdac42c0d 100644 --- a/SL/Menu.pm +++ b/SL/Menu.pm @@ -28,11 +28,24 @@ sub new { my $nodes_by_id = {}; for my $file (@files) { my $data; - if ($yaml_xs) { - $data = YAML::XS::LoadFile(File::Spec->catfile($path, $file)); - } else { - $data = YAML::LoadFile(File::Spec->catfile($path, $file)); - } + eval { + if ($yaml_xs) { + $data = YAML::XS::LoadFile(File::Spec->catfile($path, $file)); + } else { + $data = YAML::LoadFile(File::Spec->catfile($path, $file)); + } + 1; + } or do { + die "Error while parsing $file: $@"; + }; + + # check if this file is internally consistent. + die 'not an array ref' unless $data && 'ARRAY' eq ref $data; # TODO get better diag to user + + # in particular duplicate ids tend to come up as a user error when editing the menu files + my %uniq_ids; + $uniq_ids{$_->{id}}++ && die "Error in $file: duplicate id $_->{id}" for @$data; + _merge($nodes, $nodes_by_id, $data); } @@ -57,8 +70,6 @@ sub new { sub _merge { my ($nodes, $by_id, $data) = @_; - die 'not an array ref' unless $data && 'ARRAY' eq ref $data; # TODO check this sooner, to get better diag to user - for my $node (@$data) { my $id = $node->{id}; -- 2.20.1