Subject: | Better error message when a named field isn't found |
I typo'd a field name and got this Moose validation error:
Use of uninitialized value $in in numeric le (<=) at /Users/trsibley/.perlbrew/libs/5.18@hackery/lib/perl5/App/CSV.pm line 116, <$fh> line 1.
Attribute (columns) does not pass the type constraint because: Validation failed for 'ArrayRef[Int]' with value ARRAY(0x7fe50cb00ae8) at accessor App::CSV::columns (defined at /Users/trsibley/.perlbrew/libs/5.18@hackery/lib/perl5/App/CSV.pm line 26) line 4, <$fh> line 1.
App::CSV::columns('App::CSV=HASH(0x7fe50caffd80)', 'ARRAY(0x7fe50cb00ae8)') called at /Users/trsibley/.perlbrew/libs/5.18@hackery/lib/perl5/App/CSV.pm line 209
App::CSV::init('App::CSV=HASH(0x7fe50caffd80)') called at /Users/trsibley/.perlbrew/libs/5.18@hackery/lib/perl5/App/CSV.pm line 244
App::CSV::run('App::CSV=HASH(0x7fe50caffd80)') called at /Users/trsibley/.perlbrew/libs/5.18@hackery/bin/csv line 11
It's rather unfriendly and useless. I took a look at the source and saw that because I specified an unknown field name, it was passed through as undef instead of being converted to a column number.
How about this patch?
--- a/App/CSV.pm 2013-12-05 11:18:05.000000000 -0800
+++ b/App/CSV.pm 2013-12-05 11:17:51.000000000 -0800
@@ -139,6 +139,10 @@
# if there is at least one named field, we need to read the header line from input and translate into column number
if (@named_fields) {
my $header_map = $self->_get_header_map;
+ if (my @missing_fields = grep { not defined $header_map->{$_} } @named_fields) {
+ die "The following named fields aren't in the input header: ",
+ join(", ", @missing_fields), "\n";
+ }
@normalized_fields = map { __normalize_column(/^\d+/ ? $_ : $header_map->{$_} ) } @all_fields;
}
else {