Jun 022012
Article Perl

(leer este artículo en español)

In a previous post we explained how to develop perl scripts for the automated management of Google AdWords campaings, making use of a perl library that implements the API to access AdWords. During the execution of these scripts, errors of different kinds may happen: network timeouts, rejected parameter values, etc. This post will show how to catch and react to these errors.

The Adwords API Perl library employs the CPAN module SOAP::WSDL to establish the communication between the client and the server (SOAP stands for “Simple Object Access Protocol”, and WSDL stands for “Web Services Description Language”)

In a perl script that works with Adwords campaigns using the Adwords API Perl library, the modifications to be done are performed by issuing calls to the ‘mutate’ method of the service to be accessed (AdgroupService, AdgroupAdService,etc…).

If an error happens while processing the request, the return value from the ‘mutate’ method is an object of type “SOAP::WSDL::SOAP::Typelib::Fault11”.

Therefore, to detect and handle an error, we must just check the type of the returned object, and take the adequate measures in case it is of type ‘Fault11’. In the sample code below we see how this can be done:

# Don't exit in case of API error

# Create an ad
 my $anuncio = Google::Ads::AdWords::v201109::TextAd->new({

 # Add the ad to the adgroup identified by $adgroup_id
 my $adgroup_ad =
         adGroupId => $adgroup_id,
         ad => $anuncio

 # Create the operation to add the new ad
 my $operation =
         operand => $adgroup_ad,
         operator => "ADD"

 # Execute the operation
 my $result =
         operations => [$operation]

 # Check if an error happened
 if ($result->isa("SOAP::WSDL::SOAP::Typelib::Fault11")) {
     printf "Error: %sn",

Note: When used in scalar context, the value of instances of a Fault11 object is the boolean value “false”. On the other hand, when a Fault11 object is “stringified” is is converted into XML. Taking this into account, the last lines in the sample code can also be written as:

# Check if an error happened
 if (not $result)) { # Meaning that the returned object is of type Fault11
     printf "Error: %sn",$result;
 Posted by at 8:00 pm

  One Response to “How to handle errors from the Adwords API in Perl”

 Leave a Reply