File Coverage

blib/lib/PayProp/API/Public/Client/Exception/Base.pm
Criterion Covered Total %
statement 30 37 81.0
branch 6 6 100.0
condition 8 12 66.6
subroutine 9 11 81.8
pod 7 7 100.0
total 60 73 82.1


line stmt bran cond sub pod time code
1             package PayProp::API::Public::Client::Exception::Base;
2              
3 37     37   431295 use strict;
  37         72  
  37         1586  
4 37     37   195 use warnings;
  37         164  
  37         2291  
5 37     37   224 use parent qw/ Exception::Class::Base /;
  37         64  
  37         326  
6              
7 37     37   23190 use Module::Load qw//;
  37         55723  
  37         14878  
8              
9              
10 4     4 1 16 sub error_class { undef }
11              
12 0     0 1 0 sub error_fields { undef }
13              
14 7     7 1 91 sub errors { shift->{errors} }
15              
16 3     3 1 2777 sub status_code { shift->{status_code} }
17              
18 10     10 1 9666 sub Fields { qw/ status_code errors / } # for Exception::Class::Base
19              
20             sub throw {
21 10     10 1 9246 my ( $self, @args ) = @_;
22              
23 10         27 my $args_size = scalar @args;
24              
25 10 100 100     89 die 'wrong number of args for throw - expected either an error message or pairs'
26             if $args_size > 1 && $args_size % 2
27             ;
28              
29             # allow ->throw('my error message') that would otherwise cause args to be ( 'my error message' => undef )
30             # for e.g. PayProp::API::Public::Client::Exception::Connection
31 9 100       79 my %args = $args_size == 1 ? ( message => "$args[0]" ) : @args;
32              
33             # for e.g. PayProp::API::Public::Client::Exception::Authorization
34 9 100 66     53 if ( $self->error_class && $self->error_fields ) {
35 5         22 my $error_class = $self->error_class;
36 5         28 Module::Load::load( $error_class );
37              
38             $args{errors} = [
39             map {
40 3         17 my $error_ref = $_;
41              
42             $error_class->new(
43             (
44             map {
45 3   50     15 my $value = $error_ref->{ $_ } // '';
  6         23  
46 6         44 $_ => "$value";
47             } $self->error_fields
48             )
49             )
50 5   100     569 } @{ $args{errors} // [] }
  5         37  
51             ];
52             }
53              
54 9         381 $self->SUPER::throw( %args );
55             }
56              
57             sub to_hashref {
58 0     0 1   my ( $self ) = @_;
59              
60             return {
61             class => ref( $self ),
62             message => $self->{message},
63             status_code => $self->{status_code},
64             errors => [
65             map {
66 0           my $error = $_;
67             +{
68             class => ref( $error ),
69             fields => {
70 0           map { $_ => $error->{ $_ } } qw/ path code message /
  0            
71             },
72             }
73 0   0       } @{ $self->{errors} // [] }
  0            
74             ],
75             };
76             }
77              
78             1;
79              
80             __END__