Revision c46898c7
Von Sven Schöling vor fast 14 Jahren hinzugefügt
SL/Helper/Csv.pm | ||
---|---|---|
) ];
|
||
|
||
use SL::Helper::Csv::Dispatcher;
|
||
use SL::Helper::Csv::Error;
|
||
|
||
# public interface
|
||
|
||
... | ... | |
|
||
sub _push_error {
|
||
my ($self, @errors) = @_;
|
||
my @new_errors = ($self->errors, @errors);
|
||
my @new_errors = ($self->errors, map { SL::Helper::Csv::Error->new(@$_) } @errors);
|
||
$self->_errors(\@new_errors);
|
||
}
|
||
|
||
... | ... | |
=head1 ERROR HANDLING
|
||
|
||
After parsing a file all errors will be accumulated into C<errors>.
|
||
Each entry is an object with the following attributes:
|
||
|
||
Each entry is an arrayref with the following structure:
|
||
|
||
[
|
||
0 offending raw input,
|
||
1 Text::CSV error code if Text:CSV signalled an error, 0 else,
|
||
2 error diagnostics,
|
||
3 position in line,
|
||
4 estimated line in file,
|
||
]
|
||
raw_input: offending raw input,
|
||
code: Text::CSV error code if Text:CSV signalled an error, 0 else,
|
||
diag: error diagnostics,
|
||
line: position in line,
|
||
col: estimated line in file,
|
||
|
||
Note that the last entry can be off, but will give an estimate.
|
||
|
SL/Helper/Csv/Dispatcher.pm | ||
---|---|---|
_specs _errors
|
||
) ];
|
||
|
||
use SL::Helper::Csv::Error;
|
||
|
||
sub new {
|
||
my ($class, $parent) = @_;
|
||
my $self = bless { }, $class;
|
||
... | ... | |
|
||
sub _push_error {
|
||
my ($self, @errors) = @_;
|
||
my @new_errors = ($self->errors, @errors);
|
||
my @new_errors = ($self->errors, map { SL::Helper::Csv::Error->new(@$_) } @errors);
|
||
$self->_errors(\@new_errors);
|
||
}
|
||
|
SL/Helper/Csv/Error.pm | ||
---|---|---|
package SL::Helper::Csv::Error;
|
||
|
||
use strict;
|
||
|
||
sub new {
|
||
my $class = shift;
|
||
bless [ @_ ], $class;
|
||
}
|
||
|
||
sub raw_input { $_->[0] }
|
||
sub code { $_->[1] }
|
||
sub diag { $_->[2] }
|
||
sub col { $_->[3] }
|
||
sub line { $_->[4] }
|
||
|
||
1;
|
t/helper/csv.t | ||
---|---|---|
);
|
||
is $csv->parse, undef, 'broken csv content won\'t get parsed';
|
||
is_deeply $csv->errors, [ '"Kaf"fee";;0.12;1,221.52'."\n", 2023, 'EIQ - QUO character not allowed', 5, 2 ], 'error';
|
||
isa_ok( ($csv->errors)[0], 'SL::Helper::Csv::Error', 'Errors get objectified');
|
||
|
||
####
|
||
|
||
... | ... | |
);
|
||
is $csv->parse, undef, 'wrong profile gets rejected';
|
||
is_deeply $csv->errors, [ 'buchungsgruppen.1.description', undef, "Profile path error. Indexed relationship is not OneToMany around here: 'buchungsgruppen.1'", undef ,0 ], 'error indicates wrong header';
|
||
isa_ok( ($csv->errors)[0], 'SL::Helper::Csv::Error', 'Errors get objectified');
|
||
|
||
# vim: ft=perl
|
Auch abrufbar als: Unified diff
Csv Errors sind nun Objekte mit entsprechendem Zugriff.