File Coverage

lib/Google/Ads/Common/ErrorUtils.pm
Criterion Covered Total %
statement 12 19 63.1
branch 0 8 0.0
condition 0 9 0.0
subroutine 4 5 80.0
pod 1 1 100.0
total 17 42 40.4


line stmt bran cond sub pod time code
1             # Copyright 2011, Google Inc. All Rights Reserved.
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             package Google::Ads::Common::ErrorUtils;
16              
17 1     1   628 use strict;
  1         5  
  1         41  
18 1     1   9 use warnings;
  1         3  
  1         43  
19 1     1   9 use version;
  1         4  
  1         9  
20              
21             # The following needs to be on one line because CPAN uses a particularly hacky
22             # eval() to determine module versions.
23 1     1   89 use Google::Ads::Common::Constants; our $VERSION = ${Google::Ads::Common::Constants::VERSION};
  1         4  
  1         188  
24              
25             # Gets the index of the operation that was the source of an ApiError object.
26             sub get_source_operation_index ($) {
27 0     0 1   my ($self, $error) = @_;
28             # Starting in v201702, an error now includes a FieldPathElement that can
29             # return the index of the error.
30 0 0         if ($error->can('get_fieldPathElements')) {
    0          
31 0           my $field_path_elements = $error->get_fieldPathElements();
32             my $first_field_path_element =
33 0 0 0       ($field_path_elements && (scalar @{$field_path_elements} > 0))
34             ? $field_path_elements->[0]
35             : undef;
36 0 0 0       if ( $first_field_path_element
      0        
37             && ($first_field_path_element->get_field() eq "operations")
38             && defined $first_field_path_element->get_index())
39             {
40 0           return $first_field_path_element->get_index();
41             }
42             } elsif ($error->get_fieldPath() =~ /^operations\[(\d+)\]/) {
43 0           return $1;
44             }
45             }
46              
47             return 1;
48              
49             =pod
50              
51             =head1 NAME
52              
53             Google::Ads::Common::ErrorUtils
54              
55             =head1 SYNOPSIS
56              
57             use Google::Ads::Common::ErrorUtils;
58              
59             my $index =
60             Google::Ads::Common::ErrorUtils::get_source_operation_index($api_error);
61              
62             # $index will contain the index of the operation that failed in the request.
63              
64             =head1 DESCRIPTION
65              
66             =head1 SUBROUTINES
67              
68             =head2 get_source_operation_index
69              
70             Gets the index of the operation that was the source of an error.
71              
72             =head3 Parameters
73              
74             The error of type ApiError from which the index will be retrieved.
75              
76             =head3 Returns
77              
78             The index of the error or nothing if an invalid error is passed.
79              
80             =head1 LICENSE AND COPYRIGHT
81              
82             Copyright 2011 Google Inc.
83              
84             Licensed under the Apache License, Version 2.0 (the "License");
85             you may not use this file except in compliance with the License.
86             You may obtain a copy of the License at
87              
88             http://www.apache.org/licenses/LICENSE-2.0
89              
90             Unless required by applicable law or agreed to in writing, software
91             distributed under the License is distributed on an "AS IS" BASIS,
92             WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
93             See the License for the specific language governing permissions and
94             limitations under the License.
95              
96             =head1 REPOSITORY INFORMATION
97              
98             $Rev: $
99             $LastChangedBy: $
100             $Id: $
101              
102             =cut