Utils/PartialFailureUtils.pm

filter: Statementstmt 95.7% 67 / 70 Branchbran 55.5% 10 / 18 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 10 / 10 Podpod 75.0% 3 / 4 totaltotal 82.9% 97 / 117 CC 17 SCAR 18.1
CC
17
Cov
85%
get_google_ads_failure_from_status
11.5
__get_google_ads_errors
6.0
get_google_ads_errors
3.0
CRAP 6.1
« OAuth2ServiceAccountsHandler.pm ↑ Summary Logging/SummaryStats.pm »
1 # Copyright 2019, Google LLC
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 # This module contains utility methods for handling partial failure of operations.
16  
17 package Google::Ads::GoogleAds::Utils::PartialFailureUtils;
18  
19 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # The following needs to be on one line because CPAN uses a particularly hacky
24 # eval() to determine module versions.
25 2 ▶use Google::Ads::GoogleAds::Constants; our $VERSION = ${Google::Ads::GoogleAds::Constants::VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 our @EXPORT =
29 qw(is_partial_failure_result get_google_ads_errors get_google_ads_failure get_google_ads_failure_from_status);
30  
31 # Checks if a result in a mutate response is a partial failure.
32 3 ▶sub is_partial_failure_result {
Subroutine is_partial_failure_result: called CC=1 SCAR=0.0
Pod documented
33 3 my $result = shift;
34 3 return !keys %$result;
35 }
36  
37 # Returns a list of GoogleAdsError instances for a given operation index.
38 2 ▶sub get_google_ads_errors {
Subroutine get_google_ads_errors: called CC=3 SCAR=11.0
Pod documented
39 2 my ($operation_index, $partial_failure_error) = @_;
40  
41 2 my $google_ads_errors = [];
42  
43 2 foreach my $detail (@{$partial_failure_error->{details}}) {
44 2 my $google_ads_failure = get_google_ads_failure($detail);
45 2 ▶ next if not $google_ads_failure;
Branch: 1 branch
BranchTrueFalseTotal
unless $google_ads_failure 0 2 2
46  
47 push @$google_ads_errors,
48 2 @{__get_google_ads_errors($operation_index, $google_ads_failure)};
49 }
50  
51 2 return $google_ads_errors;
52 }
53  
54 # Extracts the GoogleAdsFailure instance from a partial failure detail.
55 4 ▶sub get_google_ads_failure {
Subroutine get_google_ads_failure: called CC=2 SCAR=7.1
Pod documented
56 4 my ($detail) = @_;
57  
58 4 my $type = $detail->{"\@type"};
59 4 ▶ if ($type =~ /google.ads.googleads.v(\d+).errors.GoogleAdsFailure/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /google.ads.googleads.v(\d+).errors.GoogleAdsFailure/) 4 0 4
60 4 my $class =
61 sprintf(Google::Ads::GoogleAds::Constants::GOOGLE_ADS_FAILURE_CLASS_NAME,
62 $1);
63  
64 # Require class name.
65 4 eval("require $class");
66 4 return $class->new({errors => $detail->{errors}});
67 }
68  
69 0 return undef;
70 }
71  
72 # The private method to extract a list of GoogleAdsError instances from a
73 # GoogleAdsFailure instance for a given operation index.
74 2 ▶sub __get_google_ads_errors {
Subroutine __get_google_ads_errors: called CC=6 SCAR=17.9
75 2 my ($operation_index, $google_ads_failure) = @_;
76  
77 2 my $google_ads_errors = [];
78  
79 2 ▶ if ((ref $google_ads_failure) =~ /::V(\d+)::/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($google_ads_failure) =~ /::V(\d+)::/) 2 0 2
80 2 my $class =
81 sprintf(Google::Ads::GoogleAds::Constants::GOOGLE_ADS_ERROR_CLASS_NAME,
82 $1);
83  
84 2 foreach my $error (@{$google_ads_failure->{errors}}) {
85 4 foreach my $field_path_element (@{$error->{location}{fieldPathElements}})
86 {
87 8 my $field_name = $field_path_element->{fieldName};
88 8 my $index = $field_path_element->{index};
89  
90 8 ▶ if ($field_name eq "operations" and $index == $operation_index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field_name eq "operations" and $index == $operation_index) 2 6 8
Condition100%3/3
$field_name eq "operations" and $index == $operation_index
!ll&&!rl&&r
422
MC/DC100%2/2
$field_name eq "operations" and $index == $operation_index
$field_name eq "operations" $index == $operation_index
Truth table100%3/3
A and B
A= $field_name eq "operations"B= $index == $operation_index
ABresult
0X0
100
111
91 # Bless to class name.
92 2 bless $error, $class;
93 2 push @$google_ads_errors, $error;
94 2 last;
95 }
96 }
97 }
98 }
99  
100 2 return $google_ads_errors;
101 }
102  
103 # Combines all the errors from a failed API call status into a single GoogleAdsFailure instance.
104 1 ▶sub get_google_ads_failure_from_status {
Subroutine get_google_ads_failure_from_status: called CC=9 SCAR=24.4
Pod undocumented
105 1 my ($status) = @_;
106 1 ▶ return undef if not $status;
Branch: 1 branch
BranchTrueFalseTotal
unless $status 0 1 1
107  
108 1 my $combined_errors = [];
109 1 my $version;
110 1 foreach my $detail (@{$status->{details}}) {
111 1 my $failure = get_google_ads_failure($detail);
112 1 ▶ if ($failure) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failure) 1 0 1
113 1 push @$combined_errors, @{$failure->{errors}};
114 1 ▶ if (not $version and (ref $failure) =~ /::V(\d+)::/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $version and ref($failure) =~ /::V(\d+)::/) 1 0 1
Condition33%1/3
not $version and ref($failure) =~ /::V(\d+)::/
!ll&&!rl&&r
001
MC/DC0%0/2
not $version and ref($failure) =~ /::V(\d+)::/
not $version ref($failure) =~ /::V(\d+)::/
Truth table33%1/3
not(A) and B
A= not $versionB= ref($failure) =~ /::V(\d+)::/
ABresult
0X0
100
111
115 1 $version = $1;
116 }
117 }
118 }
119  
120 1 ▶ if (scalar @$combined_errors > 0 and $version) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$combined_errors > 0 and $version) 1 0 1
Condition33%1/3
scalar @$combined_errors > 0 and $version
!ll&&!rl&&r
001
MC/DC0%0/2
scalar @$combined_errors > 0 and $version
scalar @$combined_errors > 0 $version
Truth table33%1/3
A and B
A= scalar @$combined_errors > 0B= $version
ABresult
0X0
100
111
121 1 my $class =
122 sprintf(Google::Ads::GoogleAds::Constants::GOOGLE_ADS_FAILURE_CLASS_NAME,
123 $version);
124 1 eval("require $class");
125 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
126 0 return undef;
127 }
128 1 return $class->new({errors => $combined_errors});
129 }
130 0 return undef;
131 }
132  
133 2 1;
134  
135 =pod
136  
137 =head1 NAME
138  
139 Google::Ads::GoogleAds::Utils::PartialFailureUtils
140  
141 =head1 SYNOPSIS
142  
143 use Google::Ads::GoogleAds::Utils::PartialFailureUtils;
144  
145 while (my ($operation_index, $result) = each @{$response->{results}}) {
146 if (is_partial_failure_result($result)) {
147 my $google_ads_errors = get_google_ads_errors($operation_index,
148 $response->{partialFailureError});
149  
150 foreach my $google_ads_error (@$google_ads_errors) {
151 printf "Operation %d failed with error: %s.\n", $operation_index,
152 $google_ads_error->{message};
153 }
154 } else {
155 printf "Operation %d succeeded.\n", $operation_index;
156 }
157 }
158  
159 =head1 DESCRIPTION
160  
161 This module contains utility methods for handling partial failure of operations.
162  
163 =head1 METHODS
164  
165 =head2 is_partial_failure_result
166  
167 Checks if a result in a mutate response is a partial failure.
168  
169 =head3 Parameters
170  
171 =over
172  
173 =item *
174  
175 I<result>: a B<result> hash in a mutate response.
176  
177 =back
178  
179 =head3 Returns
180  
181 True, if the result is a partial failure. False, otherwise.
182  
183 =head2 get_google_ads_errors
184  
185 Returns a list of L<Google::Ads::GoogleAds::V26::Errors::GoogleAdsError> instances
186 for a given operation index.
187  
188 =head3 Parameters
189  
190 =over
191  
192 =item *
193  
194 I<operation_index>: the index of the operation, starting from 0.
195  
196 =item *
197  
198 I<partial_failure_error>: the B<partialFailureError> hash in the mutate response,
199 with the detail list containing L<Google::Ads::GoogleAds::V26::Errors::GoogleAdsFailure>
200 instances.
201  
202 =back
203  
204 =head3 Returns
205  
206 An array containing the L<Google::Ads::GoogleAds::V26::Errors::GoogleAdsError>
207 instances for the given operation index.
208  
209 =head2 get_google_ads_failure
210  
211 Extracts the L<Google::Ads::GoogleAds::V26::Errors::GoogleAdsFailure> instance
212 from a partial failure detail.
213  
214 =head3 Parameters
215  
216 =over
217  
218 =item *
219  
220 I<detail>: an element in the B<details> hash in the mutate response.
221  
222 =back
223  
224 =head3 Returns
225  
226 A L<Google::Ads::GoogleAds::V26::Errors::GoogleAdsFailure> object or undef if not found.
227  
228 =head2 __get_google_ads_errors
229  
230 The private method to extract a list of L<Google::Ads::GoogleAds::V26::Errors::GoogleAdsError>
231 instances from a L<Google::Ads::GoogleAds::V26::Errors::GoogleAdsFailure> instance
232 for a given operation index.
233  
234 =head3 Parameters
235  
236 =over
237  
238 =item *
239  
240 I<operation_index>: the index of the operation, starting from 0.
241  
242 =item *
243  
244 I<google_ads_failure>: the L<Google::Ads::GoogleAds::V26::Errors::GoogleAdsFailure> instance.
245  
246 =back
247  
248 =head3 Returns
249  
250 An array containing the L<Google::Ads::GoogleAds::V26::Errors::GoogleAdsError>
251 instances from the L<Google::Ads::GoogleAds::V26::Errors::GoogleAdsFailure>
252 instance for the given operation index.
253  
254 =head1 LICENSE AND COPYRIGHT
255  
256 Copyright 2019 Google LLC
257  
258 Licensed under the Apache License, Version 2.0 (the "License");
259 you may not use this file except in compliance with the License.
260 You may obtain a copy of the License at
261  
262 http://www.apache.org/licenses/LICENSE-2.0
263  
264 Unless required by applicable law or agreed to in writing, software
265 distributed under the License is distributed on an "AS IS" BASIS,
266 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
267 See the License for the specific language governing permissions and
268 limitations under the License.
269  
270 =head1 REPOSITORY INFORMATION
271  
272 $Rev: $
273 $LastChangedBy: $
274 $Id: $
275  
276 =cut
« OAuth2ServiceAccountsHandler.pm ↑ Summary Logging/SummaryStats.pm »