| 1 | 1 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 3 | package Data::Rx::Failure; | ||||||||||||||||||
| 4 | # ABSTRACT: structured failure report from an Rx checker | ||||||||||||||||||
| 5 | 1 | $Data::Rx::Failure::VERSION = '0.200007'; | |||||||||||||||||
| 6 | #pod =head1 SYNOPSIS | ||||||||||||||||||
| 7 | #pod | ||||||||||||||||||
| 8 | #pod This is part of a L<Data::Rx::FailureSet>, which is what is thrown when a | ||||||||||||||||||
| 9 | #pod schema's C<assert_valid> method finds a problem with the input. For more | ||||||||||||||||||
| 10 | #pod information on it, look at the documentation for L<Data::Rx>. | ||||||||||||||||||
| 11 | #pod | ||||||||||||||||||
| 12 | #pod =cut | ||||||||||||||||||
| 13 | |||||||||||||||||||
| 14 | 1 | ▶ | use overload '""' => \&stringify; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | sub new { | ||||||||||||||||||
| 17 | 1507 | ▶ | my ($class, $arg) = @_; | ||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 18 | |||||||||||||||||||
| 19 | my $guts = { | ||||||||||||||||||
| 20 | rx => $arg->{rx}, | ||||||||||||||||||
| 21 | 1507 | struct => [ $arg->{struct} ], | |||||||||||||||||
| 22 | }; | ||||||||||||||||||
| 23 | |||||||||||||||||||
| 24 | 1507 | bless $guts => $class; | |||||||||||||||||
| 25 | } | ||||||||||||||||||
| 26 | |||||||||||||||||||
| 27 | 4780 | ▶ | sub struct { $_[0]->{struct} } | ||||||||||||||||
|
Subroutine
struct: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 28 | |||||||||||||||||||
| 29 | sub contextualize { | ||||||||||||||||||
| 30 | 253 | ▶ | my ($self, $struct) = @_; | ||||||||||||||||
|
Subroutine
contextualize: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | 253 | push @{ $self->struct }, $struct; | |||||||||||||||||
| 33 | |||||||||||||||||||
| 34 | 253 | ▶ | if (my $failures = $self->struct->[0]{failures}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 35 | 0 | $_->contextualize($struct) foreach @$failures; | |||||||||||||||||
| 36 | } | ||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | 253 | return $self; | |||||||||||||||||
| 39 | } | ||||||||||||||||||
| 40 | |||||||||||||||||||
| 41 | sub value { | ||||||||||||||||||
| 42 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||
|
Subroutine
value: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 43 | |||||||||||||||||||
| 44 | 0 | return $self->struct->[0]{value}; | |||||||||||||||||
| 45 | } | ||||||||||||||||||
| 46 | |||||||||||||||||||
| 47 | sub error_types { | ||||||||||||||||||
| 48 | 1488 | ▶ | my ($self) = @_; | ||||||||||||||||
|
Subroutine
error_types: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 49 | |||||||||||||||||||
| 50 | 1488 | return @{ $self->struct->[0]{error} }; | |||||||||||||||||
| 51 | } | ||||||||||||||||||
| 52 | |||||||||||||||||||
| 53 | sub error_string { | ||||||||||||||||||
| 54 | 47 | ▶ | my ($self) = @_; | ||||||||||||||||
|
Subroutine
error_string: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 55 | |||||||||||||||||||
| 56 | 47 | join ', ', $self->error_types; | |||||||||||||||||
| 57 | } | ||||||||||||||||||
| 58 | |||||||||||||||||||
| 59 | sub keys { | ||||||||||||||||||
| 60 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||
|
Subroutine
keys: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||
| 61 | |||||||||||||||||||
| 62 | 0 | ▶ | return @{ $self->struct->[0]{keys} || [] }; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 63 | } | ||||||||||||||||||
| 64 | |||||||||||||||||||
| 65 | sub size { | ||||||||||||||||||
| 66 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||
|
Subroutine
size: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 67 | |||||||||||||||||||
| 68 | 0 | return $self->struct->[0]{size}; | |||||||||||||||||
| 69 | } | ||||||||||||||||||
| 70 | |||||||||||||||||||
| 71 | sub data_path { | ||||||||||||||||||
| 72 | 1347 | ▶ | my ($self) = @_; | ||||||||||||||||
|
Subroutine
data_path: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 73 | |||||||||||||||||||
| 74 | 1452 | ▶ | map {; map { $_->[0] } @{ $_->{data_path} || [] } } | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 75 | 1347 | reverse @{ $self->struct }; | |||||||||||||||||
| 76 | } | ||||||||||||||||||
| 77 | |||||||||||||||||||
| 78 | sub data_string { | ||||||||||||||||||
| 79 | 47 | ▶ | my ($self) = @_; | ||||||||||||||||
|
Subroutine
data_string: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 80 | |||||||||||||||||||
| 81 | 47 | return $self->_path_string('$data', 'data_path'); | |||||||||||||||||
| 82 | } | ||||||||||||||||||
| 83 | |||||||||||||||||||
| 84 | sub check_path { | ||||||||||||||||||
| 85 | 1345 | ▶ | my ($self) = @_; | ||||||||||||||||
|
Subroutine
check_path: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 86 | |||||||||||||||||||
| 87 | 1450 | ▶ | map {; map { $_->[0] } @{ $_->{check_path} || [] } } | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 88 | 1345 | reverse @{ $self->struct }; | |||||||||||||||||
| 89 | } | ||||||||||||||||||
| 90 | |||||||||||||||||||
| 91 | sub check_string { | ||||||||||||||||||
| 92 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||
|
Subroutine
check_string: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 93 | |||||||||||||||||||
| 94 | 0 | return $self->_path_string('$schema', 'check_path'); | |||||||||||||||||
| 95 | } | ||||||||||||||||||
| 96 | |||||||||||||||||||
| 97 | sub _path_string { | ||||||||||||||||||
| 98 | 47 | ▶ | my ($self, $base, $key) = @_; | ||||||||||||||||
|
Subroutine
_path_string: called CC=7 SCAR=28.7
| |||||||||||||||||||
| 99 | |||||||||||||||||||
| 100 | 47 | my $str = $base; | |||||||||||||||||
| 101 | |||||||||||||||||||
| 102 | 47 | ▶ | for my $frame (reverse @{ $self->struct || [] }) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 103 | 47 | my $hunk = $frame->{ $key }; | |||||||||||||||||
| 104 | 47 | for my $entry (@$hunk) { | |||||||||||||||||
| 105 | 0 | ▶ | if ($entry->[1] eq 'key') { $str .= "->{$entry->[0]}"; } | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 106 | 0 | elsif ($entry->[1] eq 'index') { $str .= "->[$entry->[0]]"; } | |||||||||||||||||
| 107 | 0 | elsif ($entry->[2]) { $str = $entry->[2]->($str, @$entry) } | |||||||||||||||||
| 108 | 0 | else { $str .= "->? $entry->[0] ?"; } | |||||||||||||||||
| 109 | } | ||||||||||||||||||
| 110 | } | ||||||||||||||||||
| 111 | |||||||||||||||||||
| 112 | 47 | return $str; | |||||||||||||||||
| 113 | } | ||||||||||||||||||
| 114 | |||||||||||||||||||
| 115 | sub stringify { | ||||||||||||||||||
| 116 | 47 | ▶ | my ($self) = @_; | ||||||||||||||||
|
Subroutine
stringify: called CC=3 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||
| 117 | |||||||||||||||||||
| 118 | 47 | my $struct = $self->struct; | |||||||||||||||||
| 119 | |||||||||||||||||||
| 120 | my $str = sprintf "Failed %s: %s (error: %s at %s)", | ||||||||||||||||||
| 121 | $struct->[0]{type}, | ||||||||||||||||||
| 122 | $struct->[0]{message}, | ||||||||||||||||||
| 123 | 47 | $self->error_string, | |||||||||||||||||
| 124 | $self->data_string; | ||||||||||||||||||
| 125 | |||||||||||||||||||
| 126 | # also stringify failures under the current failure (as for //any), | ||||||||||||||||||
| 127 | # with indentation | ||||||||||||||||||
| 128 | 47 | ▶ | if (my $failures = $struct->[0]{failures}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 129 | 0 | foreach my $fail (@$failures) { | |||||||||||||||||
| 130 | 0 | my $tmp = "$fail"; | |||||||||||||||||
| 131 | 0 | $tmp =~ s/\A/ - /; | |||||||||||||||||
| 132 | 0 | $tmp =~ s/(?<=\n)^/ /mg; | |||||||||||||||||
| 133 | 0 | $str .= "\n$tmp"; | |||||||||||||||||
| 134 | } | ||||||||||||||||||
| 135 | } | ||||||||||||||||||
| 136 | |||||||||||||||||||
| 137 | 47 | return $str; | |||||||||||||||||
| 138 | } | ||||||||||||||||||
| 139 | |||||||||||||||||||
| 140 | 1 | 1; | |||||||||||||||||
| 141 | |||||||||||||||||||
| 142 | __END__ | ||||||||||||||||||