X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FMenu.pm;h=08727ce3664c58941bc58fddef04e53860bf2817;hb=28c19c1c99304a88bf3cd57eb02a4f3d502a6c62;hp=ccfc32c969213c5de5d99857969c7366e5d36772;hpb=af0085b83ecb257679c8c64324521f9515ae76b8;p=kivitendo-erp.git diff --git a/SL/Menu.pm b/SL/Menu.pm index ccfc32c96..08727ce36 100644 --- a/SL/Menu.pm +++ b/SL/Menu.pm @@ -21,18 +21,31 @@ sub new { my $path = File::Spec->catdir('menus', $domain); opendir my $dir, $path or die "can't open $path: $!"; - my @files = sort grep -f "$path/$_", readdir $dir; + my @files = sort grep -f "$path/$_", grep /\.yaml$/, readdir $dir; close $dir; my $nodes = []; 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}; @@ -97,13 +108,13 @@ sub build_tree { # first, some sanity check. are all parents valid ids or empty? for my $node ($self->nodes) { next if !exists $node->{parent} || !$node->{parent} || $self->{by_id}->{$node->{id}}; - die "menu: node $node->{id} has non-existant parent $node->{parent}"; + die "menu: node $node->{id} has non-existent parent $node->{parent}"; } my %by_parent; # order them by parent for my $node ($self->nodes) { - push @{ $by_parent{ $node->{parent} } //= [] }, $node; + push @{ $by_parent{ $node->{parent} // '' } //= [] }, $node; } my $tree = { };