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