GoCardless/Exception.pm

filter: Statementstmt 100% 73 / 73 Branchbran 100% 22 / 22 Conditioncond 74.0% 20 / 27 MC/DCmcdc 56.2% 9 / 16 Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 91.0% 142 / 156 CC 25 SCAR 17.7
CC
25
Cov
94%
__ANON__
10.0
__ANON__
9.1
conflicting_resource_id
4.0
CRAP 5.9
« GoCardless/Client.pm ↑ Summary GoCardless/Utils.pm »
1 package Business::GoCardless::Exception;
2  
3 =head1 NAME
4  
5 Business::GoCardless::Exception
6  
7 =head1 DESCRIPTION
8  
9 Exception handling for the Business::GoCardless modules, uses the Throwable
10 module.
11  
12 =cut
13  
14 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 56 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 56 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 56 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 56 ▶use Carp qw/ cluck /;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 56 with 'Throwable';
22  
23 =head1 ATTRIBUTES
24  
25 =head2 message
26  
27 The error message, if JSON is passed this will be coerced to a string.
28  
29 =head2 code
30  
31 The error code, generally the HTTP status code.
32  
33 =head2 response
34  
35 The error response, generally the HTTP response.
36  
37 The following attributes are populated from the error response of the Pro (v2)
38 API, see L<https://developer.gocardless.com/api-reference/#api-usage-errors>
39  
40 =head2 type
41  
42 The type of error, one of C<gocardless>, C<invalid_api_usage>, C<invalid_state>,
43 or C<validation_failed>.
44  
45 =head2 request_id
46  
47 The ID of the request, include this if you need to contact GoCardless support.
48  
49 =head2 documentation_url
50  
51 A link to the GoCardless documentation for the error.
52  
53 =head2 errors
54  
55 An arrayref of the individual errors, for C<validation_failed> errors these have
56 C<field> and C<message> keys, for other errors they have C<reason> and C<message>
57 keys (and possibly C<links>).
58  
59 =cut
60  
61 34 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=9 SCAR=22.1
62 34 my ( $orig,$class,@args ) = @_;
63  
64 34 my $args = $class->$orig( @args );
65  
66 # v2 API errors are JSON, so pull out the details to allow inspection
67 # of the error rather than just having it as a message string
68 34 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"message"} and not ref $args->{"message"} and $args->{"message"} =~ /^\{/) 14 20 34
Condition83%5/6
defined $args->{"message"} and not ref $args->{"message"} and $args->{"message"} =~ /^\{/
!ll&&!rl&&r
11914
defined $args->{"message"} and not ref $args->{"message"}
!ll&&!rl&&r
1033
MC/DC66%2/3
defined $args->{"message"} and not ref $args->{"message"} and $args->{"message"} =~ /^\{/
defined $args->{"message"} not ref $args->{"message"} $args->{"message"} =~ /^\{/
Truth table75%3/4
A and not(B) and C
A= defined $args->{"message"}B= not ref $args->{"message"}C= $args->{"message"} =~ /^\{/
ABCresult
0XX0
10X0
1100
1111
69 defined( $args->{message} )
70 && ! ref( $args->{message} )
71 && $args->{message} =~ /^\{/
72 ) {
73 14 my $data = eval { _decode_json( $args->{message} ) };
74  
75 14 ▶ if ( ref( $data ) eq 'HASH' && ref( $data->{error} ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "HASH" and ref $data->{"error"} eq "HASH") 11 3 14
Condition100%3/3
ref $data eq "HASH" and ref $data->{"error"} eq "HASH"
!ll&&!rl&&r
1211
MC/DC100%2/2
ref $data eq "HASH" and ref $data->{"error"} eq "HASH"
ref $data eq "HASH" ref $data->{"error"} eq "HASH"
Truth table100%3/3
A and B
A= ref $data eq "HASH"B= ref $data->{"error"} eq "HASH"
ABresult
0X0
100
111
76 11 my $error = $data->{error};
77 11 ▶ $args->{$_} //= $error->{$_} for qw/ type request_id documentation_url /;
Condition66%2/3
$args->{$_} //= $error->{$_}
l!l&&r!l&&!r
02211
MC/DC50%1/2
$args->{$_} //= $error->{$_}
$args->{$_} $error->{$_}
Truth table66%2/3
A //= B
A= $args->{$_}B= $error->{$_}
ABresult
1X1
011
000
78 11 ▶ $args->{errors} //= $error->{errors};
Condition66%2/3
$args->{"errors"} //= $error->{"errors"}
l!l&&r!l&&!r
0101
MC/DC50%1/2
$args->{"errors"} //= $error->{"errors"}
$args->{"errors"} $error->{"errors"}
Truth table66%2/3
A //= B
A= $args->{"errors"}B= $error->{"errors"}
ABresult
1X1
011
000
79 }
80 }
81  
82 34 return $args;
83 56 };
84  
85 # plain string or JSON
86 has message => (
87 is => 'ro',
88 required => 1,
89 33 ▶ coerce => sub {
Subroutine __ANON__: called CC=10 SCAR=23.0
90 33 my ( $message ) = @_;
91  
92 33 ▶ cluck $message if $ENV{GOCARDLESS_DEV_TESTING};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"GOCARDLESS_DEV_TESTING"} 2 31 33
93  
94 33 ▶ if ( $message =~ /^[{\[]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message =~ /^[{\[]/) { } 15 18 33
95 # defensive decoding
96 15 eval { $message = _decode_json( $message ) };
97 15 ▶ $@ && do { return "Failed to parse JSON response ($message): $@"; };
Branch: 1 branch
BranchTrueFalseTotal
$@ and do { return "Failed to parse JSON response ($message): $@" } 1 14 15
98  
99 14 ▶ if ( ref( $message ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $message eq 'HASH') { } 13 1 14
100 13 ▶ my $error = delete( $message->{error} ) // "Unknown error";
Condition50%1/2
delete $message->{"error"} // "Unknown error"
l!l
130
MC/DC0%0/1
delete $message->{"error"} // "Unknown error"
delete $message->{"error"}
Truth table50%1/2
A
A= delete $message->{"error"}
Aresult
11
00
101  
102 13 ▶ if ( ref( $error ) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $error eq 'HASH') { } 11 2 13
elsif (ref $error eq 'ARRAY') { } 1 1 2
103  
104 11 my $mess = $error->{message};
105  
106 11 ▶ foreach my $sub_error ( @{ $error->{errors} // [] } ) {
Condition100%2/2
$error->{"errors"} // []
l!l
101
MC/DC100%1/1
$error->{"errors"} // []
$error->{"errors"}
Truth table100%2/2
A
A= $error->{"errors"}
Aresult
11
00
107 7 $mess .= ' / ' . $sub_error->{message};
108 }
109  
110 11 return $mess . " << $mess >> ";
111  
112 } elsif ( ref( $error ) eq 'ARRAY' ) {
113 1 return join( ', ',@{ $error } );
114 } else {
115 1 return $error;
116 }
117  
118 } else {
119 1 return join( ', ',@{ $message } );
120 }
121 } else {
122 18 return $message;
123 }
124 },
125 56 );
126  
127 # generally the HTTP status code
128 56 has code => (
129 is => 'ro',
130 required => 0,
131 );
132  
133 # generally the HTTP status code + message
134 56 has response => (
135 is => 'ro',
136 required => 0,
137 );
138  
139 56 has [ qw/
140 type
141 request_id
142 documentation_url
143 / ] => (
144 is => 'ro',
145 required => 0,
146 );
147  
148 has errors => (
149 is => 'ro',
150 required => 0,
151 33 ▶ coerce => sub { ref( $_[0] ) eq 'ARRAY' ? $_[0] : [] },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 32 1 33
152 22 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
153 56 );
154  
155 # error responses are UTF-8 encoded bytes, but we may have been passed a
156 # message that has already been decoded. work out which it is before decoding
157 # so any failure to decode is reported as is, rather than retrying in the
158 # other mode and masking the real error. note a string of characters that is
159 # also valid UTF-8 (e.g. "ΓΈ") is ambiguous, and treated as UTF-8 bytes
160 29 ▶sub _decode_json {
Subroutine _decode_json: called CC=3 SCAR=11.0
161 29 my ( $json ) = @_;
162  
163 29 ▶ my $is_utf8_bytes = $json !~ /[^\x00-\xFF]/
Condition100%3/3
!($json =~ /[^\x00-\xFF]/) && utf8::decode(my $copy = $json)
!ll&&!rl&&r
2225
MC/DC100%2/2
!($json =~ /[^\x00-\xFF]/) && utf8::decode(my $copy = $json)
!($json =~ /[^\x00-\xFF]/) utf8::decode(my $copy = $json)
Truth table100%3/3
not(A) && B
A= !($json =~ /[^\x00-\xFF]/)B= utf8::decode(my $copy = $json)
ABresult
0X0
100
111
164 && utf8::decode( my $copy = $json );
165  
166 29 ▶ return JSON->new->utf8( $is_utf8_bytes ? 1 : 0 )->decode( $json );
Branch: 1 branch
BranchTrueFalseTotal
$is_utf8_bytes ? : 25 4 29
167 }
168  
169 =head1 METHODS
170  
171 =head2 description
172  
173 An alias to the message attribute.
174  
175 =head2 reasons
176  
177 Returns a list of the C<reason> values from the L</errors>:
178  
179 my @reasons = $Exception->reasons;
180  
181 =head2 has_reason
182  
183 Returns true if any of the L</errors> have the given reason:
184  
185 if ( $Exception->has_reason( 'mandate_is_inactive' ) ) {
186 ...
187 }
188  
189 =head2 conflicting_resource_id
190  
191 Returns the ID of the resource already created with the idempotency key used
192 in the request, if the error is an C<idempotent_creation_conflict>.
193  
194 =cut
195  
196 # compatibility with ruby lib
197 1 ▶sub description { shift->message }
Subroutine description: called CC=1 SCAR=0.0
Pod documented
198  
199 7 ▶sub reasons {
Subroutine reasons: called CC=2 SCAR=7.4
Pod documented
200 7 my ( $self ) = @_;
201 7 ▶ return map { $_->{reason} // () } @{ $self->errors };
Condition33%1/3
$_->{"reason"} // ()
l!l&&r!l&&!r
800
MC/DC0%0/2
$_->{"reason"} // ()
$_->{"reason"} ()
Truth table33%1/3
A // B
A= $_->{"reason"}B= ()
ABresult
1X1
011
000
202 }
203  
204 4 ▶sub has_reason {
Subroutine has_reason: called CC=1 SCAR=0.0
Pod documented
205 4 my ( $self,$reason ) = @_;
206 4 return scalar( grep { $_ eq $reason } $self->reasons );
207 }
208  
209 4 ▶sub conflicting_resource_id {
Subroutine conflicting_resource_id: called CC=4 SCAR=13.9
Pod documented
210 4 my ( $self ) = @_;
211  
212 4 foreach my $error ( @{ $self->errors } ) {
213 5 ▶ next if ( $error->{reason} // '' ) ne 'idempotent_creation_conflict';
Branch: 1 branch
BranchTrueFalseTotal
if ($error->{"reason"} // "") ne "idempotent_creation_conflict" 2 3 5
Condition50%1/2
$error->{"reason"} // ""
l!l
50
MC/DC0%0/1
$error->{"reason"} // ""
$error->{"reason"}
Truth table50%1/2
A
A= $error->{"reason"}
Aresult
11
00
214 3 return $error->{links}{conflicting_resource_id};
215 }
216  
217 1 return;
218 }
219  
220 =head1 AUTHOR
221  
222 Lee Johnson - C<lee@payprop.com>
223  
224 This library is free software; you can redistribute it and/or modify it under
225 the same terms as Perl itself. If you would like to contribute documentation,
226 features, bug fixes, or anything else then please raise an issue / pull request:
227  
228 https://github.com/payprop/business-gocardless
229  
230 =cut
231  
232 56 1;
233  
234 # vim: ts=4:sw=4:et
« GoCardless/Client.pm ↑ Summary GoCardless/Utils.pm »