Revision c46898c7
Von Sven Schöling vor mehr als 13 Jahren hinzugefügt
SL/Helper/Csv.pm | ||
---|---|---|
14 | 14 |
) ]; |
15 | 15 |
|
16 | 16 |
use SL::Helper::Csv::Dispatcher; |
17 |
use SL::Helper::Csv::Error; |
|
17 | 18 |
|
18 | 19 |
# public interface |
19 | 20 |
|
... | ... | |
180 | 181 |
|
181 | 182 |
sub _push_error { |
182 | 183 |
my ($self, @errors) = @_; |
183 |
my @new_errors = ($self->errors, @errors); |
|
184 |
my @new_errors = ($self->errors, map { SL::Helper::Csv::Error->new(@$_) } @errors);
|
|
184 | 185 |
$self->_errors(\@new_errors); |
185 | 186 |
} |
186 | 187 |
|
... | ... | |
346 | 347 |
=head1 ERROR HANDLING |
347 | 348 |
|
348 | 349 |
After parsing a file all errors will be accumulated into C<errors>. |
350 |
Each entry is an object with the following attributes: |
|
349 | 351 |
|
350 |
Each entry is an arrayref with the following structure: |
|
351 |
|
|
352 |
[ |
|
353 |
0 offending raw input, |
|
354 |
1 Text::CSV error code if Text:CSV signalled an error, 0 else, |
|
355 |
2 error diagnostics, |
|
356 |
3 position in line, |
|
357 |
4 estimated line in file, |
|
358 |
] |
|
352 |
raw_input: offending raw input, |
|
353 |
code: Text::CSV error code if Text:CSV signalled an error, 0 else, |
|
354 |
diag: error diagnostics, |
|
355 |
line: position in line, |
|
356 |
col: estimated line in file, |
|
359 | 357 |
|
360 | 358 |
Note that the last entry can be off, but will give an estimate. |
361 | 359 |
|
SL/Helper/Csv/Dispatcher.pm | ||
---|---|---|
9 | 9 |
_specs _errors |
10 | 10 |
) ]; |
11 | 11 |
|
12 |
use SL::Helper::Csv::Error; |
|
13 |
|
|
12 | 14 |
sub new { |
13 | 15 |
my ($class, $parent) = @_; |
14 | 16 |
my $self = bless { }, $class; |
... | ... | |
137 | 139 |
|
138 | 140 |
sub _push_error { |
139 | 141 |
my ($self, @errors) = @_; |
140 |
my @new_errors = ($self->errors, @errors); |
|
142 |
my @new_errors = ($self->errors, map { SL::Helper::Csv::Error->new(@$_) } @errors);
|
|
141 | 143 |
$self->_errors(\@new_errors); |
142 | 144 |
} |
143 | 145 |
|
SL/Helper/Csv/Error.pm | ||
---|---|---|
1 |
package SL::Helper::Csv::Error; |
|
2 |
|
|
3 |
use strict; |
|
4 |
|
|
5 |
sub new { |
|
6 |
my $class = shift; |
|
7 |
bless [ @_ ], $class; |
|
8 |
} |
|
9 |
|
|
10 |
sub raw_input { $_->[0] } |
|
11 |
sub code { $_->[1] } |
|
12 |
sub diag { $_->[2] } |
|
13 |
sub col { $_->[3] } |
|
14 |
sub line { $_->[4] } |
|
15 |
|
|
16 |
1; |
t/helper/csv.t | ||
---|---|---|
125 | 125 |
); |
126 | 126 |
is $csv->parse, undef, 'broken csv content won\'t get parsed'; |
127 | 127 |
is_deeply $csv->errors, [ '"Kaf"fee";;0.12;1,221.52'."\n", 2023, 'EIQ - QUO character not allowed', 5, 2 ], 'error'; |
128 |
isa_ok( ($csv->errors)[0], 'SL::Helper::Csv::Error', 'Errors get objectified'); |
|
128 | 129 |
|
129 | 130 |
#### |
130 | 131 |
|
... | ... | |
222 | 223 |
); |
223 | 224 |
is $csv->parse, undef, 'wrong profile gets rejected'; |
224 | 225 |
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'; |
226 |
isa_ok( ($csv->errors)[0], 'SL::Helper::Csv::Error', 'Errors get objectified'); |
|
225 | 227 |
|
226 | 228 |
# vim: ft=perl |
Auch abrufbar als: Unified diff
Csv Errors sind nun Objekte mit entsprechendem Zugriff.