File Coverage

blib/lib/Google/Ads/GoogleAds/Utils/PartialFailureUtils.pm
Criterion Covered Total %
statement 47 48 97.9
branch 5 8 62.5
condition 3 3 100.0
subroutine 9 9 100.0
pod 3 3 100.0
total 67 71 94.3


line stmt bran cond sub pod time code
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              
18             use strict;
19 2     2   1843 use warnings;
  2         5  
  2         49  
20 2     2   10 use version;
  2         4  
  2         51  
21 2     2   10  
  2         4  
  2         12  
22             # The following needs to be on one line because CPAN uses a particularly hacky
23             # eval() to determine module versions.
24             use Google::Ads::GoogleAds::Constants; our $VERSION = ${Google::Ads::GoogleAds::Constants::VERSION};
25 2     2   152  
  2         11  
  2         97  
26             use Exporter 'import';
27 2     2   10 our @EXPORT =
  2         5  
  2         919  
28             qw(is_partial_failure_result get_google_ads_errors get_google_ads_failure);
29              
30             # Checks if a result in a mutate response is a partial failure.
31             my $result = shift;
32             return !keys %$result;
33 3     3 1 581 }
34 3         18  
35             # Returns a list of GoogleAdsError instances for a given operation index.
36             my ($operation_index, $partial_failure_error) = @_;
37              
38             my $google_ads_errors = [];
39 2     2 1 4968  
40             foreach my $detail (@{$partial_failure_error->{details}}) {
41 2         4 my $google_ads_failure = get_google_ads_failure($detail);
42             next if not $google_ads_failure;
43 2         3  
  2         5  
44 2         4 push @$google_ads_errors,
45 2 50       5 @{__get_google_ads_errors($operation_index, $google_ads_failure)};
46             }
47              
48 2         3 return $google_ads_errors;
  2         4  
49             }
50              
51 2         5 # Extracts the GoogleAdsFailure instance from a partial failure detail.
52             my ($detail) = @_;
53              
54             my $type = $detail->{"\@type"};
55             if ($type =~ /google.ads.googleads.v(\d+).errors.GoogleAdsFailure/) {
56 3     3 1 2043 my $class =
57             sprintf(Google::Ads::GoogleAds::Constants::GOOGLE_ADS_FAILURE_CLASS_NAME,
58 3         7 $1);
59 3 50       22  
60 3         18 # Require class name.
61             eval("require $class");
62             return $class->new({errors => $detail->{errors}});
63             }
64              
65 3         137 return undef;
66 3         22 }
67              
68             # The private method to extract a list of GoogleAdsError instances from a
69 0         0 # GoogleAdsFailure instance for a given operation index.
70             my ($operation_index, $google_ads_failure) = @_;
71              
72             my $google_ads_errors = [];
73              
74             if ((ref $google_ads_failure) =~ /::V(\d+)::/) {
75 2     2   3 my $class =
76             sprintf(Google::Ads::GoogleAds::Constants::GOOGLE_ADS_ERROR_CLASS_NAME,
77 2         4 $1);
78              
79 2 50       9 foreach my $error (@{$google_ads_failure->{errors}}) {
80 2         9 foreach my $field_path_element (@{$error->{location}{fieldPathElements}})
81             {
82             my $field_name = $field_path_element->{fieldName};
83             my $index = $field_path_element->{index};
84 2         3  
  2         4  
85 4         7 if ($field_name eq "operations" and $index == $operation_index) {
  4         6  
86             # Bless to class name.
87 8         10 bless $error, $class;
88 8         10 push @$google_ads_errors, $error;
89             last;
90 8 100 100     29 }
91             }
92 2         9 }
93 2         2 }
94 2         4  
95             return $google_ads_errors;
96             }
97              
98             1;
99              
100 2         9 =pod
101              
102             =head1 NAME
103              
104             Google::Ads::GoogleAds::Utils::PartialFailureUtils
105              
106             =head1 SYNOPSIS
107              
108             use Google::Ads::GoogleAds::Utils::PartialFailureUtils;
109              
110             while (my ($operation_index, $result) = each @{$response->{results}}) {
111             if (is_partial_failure_result($result)) {
112             my $google_ads_errors = get_google_ads_errors($operation_index,
113             $response->{partialFailureError});
114              
115             foreach my $google_ads_error (@$google_ads_errors) {
116             printf "Operation %d failed with error: %s.\n", $operation_index,
117             $google_ads_error->{message};
118             }
119             } else {
120             printf "Operation %d succeeded.\n", $operation_index;
121             }
122             }
123              
124             =head1 DESCRIPTION
125              
126             This module contains utility methods for handling partial failure of operations.
127              
128             =head1 METHODS
129              
130             =head2 is_partial_failure_result
131              
132             Checks if a result in a mutate response is a partial failure.
133              
134             =head3 Parameters
135              
136             =over
137              
138             =item *
139              
140             I<result>: a B<result> hash in a mutate response.
141              
142             =back
143              
144             =head3 Returns
145              
146             True, if the result is a partial failure. False, otherwise.
147              
148             =head2 get_google_ads_errors
149              
150             Returns a list of L<Google::Ads::GoogleAds::V11::Errors::GoogleAdsError> instances
151             for a given operation index.
152              
153             =head3 Parameters
154              
155             =over
156              
157             =item *
158              
159             I<operation_index>: the index of the operation, starting from 0.
160              
161             =item *
162              
163             I<partial_failure_error>: the B<partialFailureError> hash in the mutate response,
164             with the detail list containing L<Google::Ads::GoogleAds::V11::Errors::GoogleAdsFailure>
165             instances.
166              
167             =back
168              
169             =head3 Returns
170              
171             An array containing the L<Google::Ads::GoogleAds::V11::Errors::GoogleAdsError>
172             instances for the given operation index.
173              
174             =head2 get_google_ads_failure
175              
176             Extracts the L<Google::Ads::GoogleAds::V11::Errors::GoogleAdsFailure> instance
177             from a partial failure detail.
178              
179             =head3 Parameters
180              
181             =over
182              
183             =item *
184              
185             I<detail>: an element in the B<details> hash in the mutate response.
186              
187             =back
188              
189             =head3 Returns
190              
191             A L<Google::Ads::GoogleAds::V11::Errors::GoogleAdsFailure> object or undef if not found.
192              
193             =head2 __get_google_ads_errors
194              
195             The private method to extract a list of L<Google::Ads::GoogleAds::V11::Errors::GoogleAdsError>
196             instances from a L<Google::Ads::GoogleAds::V11::Errors::GoogleAdsFailure> instance
197             for a given operation index.
198              
199             =head3 Parameters
200              
201             =over
202              
203             =item *
204              
205             I<operation_index>: the index of the operation, starting from 0.
206              
207             =item *
208              
209             I<google_ads_failure>: the L<Google::Ads::GoogleAds::V11::Errors::GoogleAdsFailure> instance.
210              
211             =back
212              
213             =head3 Returns
214              
215             An array containing the L<Google::Ads::GoogleAds::V11::Errors::GoogleAdsError>
216             instances from the L<Google::Ads::GoogleAds::V11::Errors::GoogleAdsFailure>
217             instance for the given operation index.
218              
219             =head1 LICENSE AND COPYRIGHT
220              
221             Copyright 2019 Google LLC
222              
223             Licensed under the Apache License, Version 2.0 (the "License");
224             you may not use this file except in compliance with the License.
225             You may obtain a copy of the License at
226              
227             http://www.apache.org/licenses/LICENSE-2.0
228              
229             Unless required by applicable law or agreed to in writing, software
230             distributed under the License is distributed on an "AS IS" BASIS,
231             WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
232             See the License for the specific language governing permissions and
233             limitations under the License.
234              
235             =head1 REPOSITORY INFORMATION
236              
237             $Rev: $
238             $LastChangedBy: $
239             $Id: $
240              
241             =cut