Error Handling Samples

The code samples below provide examples of common error handling functions using the AdWords API. Client Library.

Handle partial failures

' Copyright 2017, Google Inc. All Rights Reserved.
'
' Licensed under the Apache License, Version 2.0 (the "License");
' you may not use this file except in compliance with the License.
' You may obtain a copy of the License at
'
'     http://www.apache.org/licenses/LICENSE-2.0
'
' Unless required by applicable law or agreed to in writing, software
' distributed under the License is distributed on an "AS IS" BASIS,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' See the License for the specific language governing permissions and
' limitations under the License.

Imports Google.Api.Ads.AdWords.Lib
Imports Google.Api.Ads.AdWords.Util
Imports Google.Api.Ads.AdWords.v201705

Imports System
Imports System.Collections.Generic
Imports System.IO

Namespace Google.Api.Ads.AdWords.Examples.VB.v201705
  ''' <summary>
  ''' This code example demonstrates how to handle partial failures.
  ''' </summary>
  Public Class HandlePartialFailures
    Inherits ExampleBase
    ''' <summary>
    ''' Main method, to run this code example as a standalone application.
    ''' </summary>
    ''' <param name="args">The command line arguments.</param>
    Public Shared Sub Main(ByVal args As String())
      Dim codeExample As New HandlePartialFailures
      Console.WriteLine(codeExample.Description)
      Try
        Dim adGroupId As Long = Long.Parse("INSERT_ADGROUP_ID_HERE")
        codeExample.Run(New AdWordsUser, adGroupId)
      Catch e As Exception
        Console.WriteLine("An exception occurred while running this code example. {0}", _
            ExampleUtilities.FormatException(e))
      End Try
    End Sub

    ''' <summary>
    ''' Returns a description about the code example.
    ''' </summary>
    Public Overrides ReadOnly Property Description() As String
      Get
        Return "This code example demonstrates how to handle partial failures."
      End Get
    End Property

    ''' <summary>
    ''' Runs the code example.
    ''' </summary>
    ''' <param name="user">The AdWords user.</param>
    ''' <param name="adGroupId">Id of the ad group to which keywords are added.
    ''' </param>
    Public Sub Run(ByVal user As AdWordsUser, ByVal adGroupId As Long)
      ' Get the AdGroupCriterionService.
      Dim adGroupCriterionService As AdGroupCriterionService = CType(user.GetService( _
          AdWordsService.v201705.AdGroupCriterionService), AdGroupCriterionService)

      ' Set partial failure mode for the service.
      adGroupCriterionService.RequestHeader.partialFailure = True

      Dim operations As New List(Of AdGroupCriterionOperation)

      ' Create the keywords.
      Dim keywords As String() = New String() {"mars cruise", "inv@lid cruise", "venus cruise", _
          "b(a)d keyword cruise"}

      For Each keywordText As String In keywords
        Dim keyword As New Keyword
        keyword.text = keywordText
        keyword.matchType = KeywordMatchType.BROAD

        ' Create biddable ad group criterion.
        Dim keywordBiddableAdGroupCriterion As New BiddableAdGroupCriterion
        keywordBiddableAdGroupCriterion.adGroupId = adGroupId
        keywordBiddableAdGroupCriterion.criterion = keyword

        ' Create the operation.
        Dim keywordAdGroupCriterionOperation As New AdGroupCriterionOperation
        keywordAdGroupCriterionOperation.operand = keywordBiddableAdGroupCriterion
        keywordAdGroupCriterionOperation.operator = [Operator].ADD
        operations.Add(keywordAdGroupCriterionOperation)
      Next

      Try
        ' Create the keywords.
        Dim result As AdGroupCriterionReturnValue = adGroupCriterionService.mutate( _
            operations.ToArray)

        ' Display the results.
        If ((Not result Is Nothing) AndAlso (Not result.value Is Nothing)) Then
          For Each adGroupCriterionResult As AdGroupCriterion In result.value
            If (Not adGroupCriterionResult.criterion Is Nothing) Then
              Console.WriteLine("Keyword with ad group id '{0}', and criterion id " & _
                  "'{1}', and text '{2}' was added.\n", adGroupCriterionResult.adGroupId, _
                  adGroupCriterionResult.criterion.id, _
                  DirectCast(adGroupCriterionResult.criterion, Keyword).text)
            End If
          Next
        Else
          Console.WriteLine("No keywords were added.")
        End If

        ' Display the partial failure errors.
        If ((Not result Is Nothing) AndAlso (Not result.partialFailureErrors Is Nothing)) Then
          For Each apiError As ApiError In result.partialFailureErrors
            Dim operationIndex As Integer = apiError.GetOperationIndex()
            If (operationIndex <> -1) Then
              Dim adGroupCriterion As AdGroupCriterion = operations(operationIndex).operand
              Console.WriteLine("Keyword with ad group id '{0}' and text '{1}' " & _
                  "triggered a failure for the following reason: '{2}'.\n", _
                  adGroupCriterion.adGroupId, _
                  DirectCast(adGroupCriterion.criterion, Keyword).text, _
                  apiError.errorString)
            Else
              Console.WriteLine("A failure for the following reason: '{0}' has occurred.\n", _
                  apiError.errorString)
            End If
          Next
        End If
      Catch e As Exception
        Throw New System.ApplicationException("Failed to add keyword(s) in partial failure mode.", _
            e)
      End Try
    End Sub
  End Class
End Namespace

Handle policy violation errors

' Copyright 2017, Google Inc. All Rights Reserved.
'
' Licensed under the Apache License, Version 2.0 (the "License");
' you may not use this file except in compliance with the License.
' You may obtain a copy of the License at
'
'     http://www.apache.org/licenses/LICENSE-2.0
'
' Unless required by applicable law or agreed to in writing, software
' distributed under the License is distributed on an "AS IS" BASIS,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' See the License for the specific language governing permissions and
' limitations under the License.

Imports Google.Api.Ads.AdWords.Lib
Imports Google.Api.Ads.AdWords.Util
Imports Google.Api.Ads.AdWords.v201705

Imports System
Imports System.Collections.Generic
Imports System.IO

Namespace Google.Api.Ads.AdWords.Examples.VB.v201705
  ''' <summary>
  ''' This code example adds a text ad, and shows how to handle a policy
  ''' violation.
  ''' </summary>
  Public Class HandlePolicyViolationError
    Inherits ExampleBase
    ''' <summary>
    ''' Main method, to run this code example as a standalone application.
    ''' </summary>
    ''' <param name="args">The command line arguments.</param>
    Public Shared Sub Main(ByVal args As String())
      Dim codeExample As New HandlePolicyViolationError
      Console.WriteLine(codeExample.Description)
      Try
        Dim adGroupId As Long = Long.Parse("INSERT_ADGROUP_ID_HERE")
        codeExample.Run(New AdWordsUser, adGroupId)
      Catch e As Exception
        Console.WriteLine("An exception occurred while running this code example. {0}", _
            ExampleUtilities.FormatException(e))
      End Try
    End Sub

    ''' <summary>
    ''' Returns a description about the code example.
    ''' </summary>
    Public Overrides ReadOnly Property Description() As String
      Get
        Return "This code example adds a text ad, and shows how to handle a policy violation."
      End Get
    End Property

    ''' <summary>
    ''' Runs the code example.
    ''' </summary>
    ''' <param name="user">The AdWords user.</param>
    ''' <param name="adGroupId">Id of the ad group to which ads are added.
    ''' </param>
    Public Sub Run(ByVal user As AdWordsUser, ByVal adGroupId As Long)
      ' Get the AdGroupAdService.
      Dim service As AdGroupAdService = CType(user.GetService( _
          AdWordsService.v201705.AdGroupAdService), AdGroupAdService)

      ' Create the text ad.
      Dim textAd As New TextAd
      textAd.headline = "Luxury Cruise to Mars"
      textAd.description1 = "Visit the Red Planet in style."
      textAd.description2 = "Low-gravity fun for everyone!!"
      textAd.displayUrl = "www.example.com"
      textAd.url = "http://www.example.com"

      Dim textadGroupAd As New AdGroupAd
      textadGroupAd.adGroupId = adGroupId
      textadGroupAd.ad = textAd

      ' Create the operations.
      Dim textAdOperation As New AdGroupAdOperation
      textAdOperation.operator = [Operator].ADD
      textAdOperation.operand = textadGroupAd

      Try
        Dim retVal As AdGroupAdReturnValue = Nothing

        ' Setup two arrays, one to hold the list of all operations to be
        ' validated, and another to hold the list of operations that cannot be
        ' fixed after validation.
        Dim allOperations As New List(Of AdGroupAdOperation)
        Dim operationsToBeRemoved As New List(Of AdGroupAdOperation)

        allOperations.Add(textAdOperation)

        Try
          ' Validate the operations.
          service.RequestHeader.validateOnly = True
          retVal = service.mutate(allOperations.ToArray)
        Catch e As AdWordsApiException
          Dim innerException As ApiException = TryCast(e.ApiException, ApiException)
          If (innerException Is Nothing) Then
            Throw New Exception("Failed to retrieve ApiError. See inner exception for more " & _
                "details.", e)
          End If

          ' Examine each ApiError received from the server.
          For Each apiError As ApiError In innerException.errors
            Dim index As Integer = apiError.GetOperationIndex()
            If (index = -1) Then
              ' This API error is not associated with an operand, so we cannot
              ' recover from this error by removing one or more operations.
              ' Rethrow the exception for manual inspection.
              Throw
            End If

            ' Handle policy violation errors.
            If TypeOf apiError Is PolicyViolationError Then
              Dim policyError As PolicyViolationError = CType(apiError, PolicyViolationError)

              If policyError.isExemptable Then
                ' If the policy violation error is exemptable, add an exemption
                ' request.
                Dim exemptionRequests As New List(Of ExemptionRequest)
                If (Not allOperations.Item(index).exemptionRequests Is Nothing) Then
                  exemptionRequests.AddRange(allOperations.Item(index).exemptionRequests)
                End If

                Dim exemptionRequest As New ExemptionRequest
                exemptionRequest.key = policyError.key
                exemptionRequests.Add(exemptionRequest)
                allOperations.Item(index).exemptionRequests = exemptionRequests.ToArray
              Else
                ' Policy violation error is not exemptable, remove this
                ' operation from the list of operations.
                operationsToBeRemoved.Add(allOperations.Item(index))
              End If
            Else
              ' This is not a policy violation error, remove this operation
              ' from the list of operations.
              operationsToBeRemoved.Add(allOperations.Item(index))
            End If
          Next

          ' Remove all operations that aren't exemptable.
          For Each operation As AdGroupAdOperation In operationsToBeRemoved
            allOperations.Remove(operation)
          Next
        End Try
        If (allOperations.Count > 0) Then
          ' Perform the operations exemptible of a policy violation.
          service.RequestHeader.validateOnly = False
          retVal = service.mutate(allOperations.ToArray)

          ' Display the results.
          If ((Not retVal Is Nothing) AndAlso (Not retVal.value Is Nothing) _
              AndAlso (retVal.value.Length > 0)) Then
            For Each newAdGroupAd As AdGroupAd In retVal.value
              Console.WriteLine("New ad with id = ""{0}"" and displayUrl = ""{1}"" was created.", _
                  newAdGroupAd.ad.id, newAdGroupAd.ad.displayUrl)
            Next
          Else
            Console.WriteLine("No ads were created.")
          End If
        Else
          Console.WriteLine("There are no ads to create after policy violation checks.")
        End If
      Catch e As Exception
        Throw New System.ApplicationException("Failed to create Ad(s).", e)
      End Try
    End Sub
  End Class
End Namespace

Handle rate exceeded error

' Copyright 2017, Google Inc. All Rights Reserved.
'
' Licensed under the Apache License, Version 2.0 (the "License");
' you may not use this file except in compliance with the License.
' You may obtain a copy of the License at
'
'     http://www.apache.org/licenses/LICENSE-2.0
'
' Unless required by applicable law or agreed to in writing, software
' distributed under the License is distributed on an "AS IS" BASIS,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' See the License for the specific language governing permissions and
' limitations under the License.

Imports Google.Api.Ads.AdWords.Lib
Imports Google.Api.Ads.AdWords.v201705

Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Net
Imports System.Threading

Namespace Google.Api.Ads.AdWords.Examples.VB.v201705
  ''' <summary>
  ''' This code example shows how to handle RateExceededError in your
  ''' application. To trigger the rate exceeded error, this code example runs
  ''' 100 threads in parallel, each thread attempting to validate 100 keywords
  ''' in a single request. Note that spawning 100 parallel threads is for
  ''' illustrative purposes only, you shouldn't do this in your application.
  ''' </summary>
  Public Class HandleRateExceededError
    Inherits ExampleBase
    ''' <summary>
    ''' Main method, to run this code example as a standalone application.
    ''' </summary>
    ''' <param name="args">The command line arguments.</param>
    Public Shared Sub Main(ByVal args As String())
      Dim codeExample As New HandleRateExceededError
      Console.WriteLine(codeExample.Description)
      Try
        Dim adGroupId As Long = Long.Parse("INSERT_ADGROUP_ID_HERE")
        codeExample.Run(New AdWordsUser, adGroupId)
      Catch e As Exception
        Console.WriteLine("An exception occurred while running this code example. {0}", _
            ExampleUtilities.FormatException(e))
      End Try
    End Sub

    ''' <summary>
    ''' Returns a description about the code example.
    ''' </summary>
    Public Overrides ReadOnly Property Description() As String
      Get
        Return "This code example shows how to handle RateExceededError in your application. " & _
            "To trigger the rate exceeded error, this code example runs 100 threads in " & _
            "parallel, each thread attempting to validate 100 keywords in a single request. " & _
            "Note that spawning 100 parallel threads is for illustrative purposes only, you " & _
            "shouldn't do this in your application."
      End Get
    End Property

    ''' <summary>
    ''' Runs the code example.
    ''' </summary>
    ''' <param name="user">The AdWords user.</param>
    ''' <param name="adGroupId">Id of the ad group to which keywords are added.
    ''' </param>
    Public Sub Run(ByVal user As AdWordsUser, ByVal adGroupId As Long)
      Const NUM_THREADS As Integer = 100

      ' Increase the maximum number of parallel HTTP connections that .NET
      ' framework allows. By default, this is set to 2 by the .NET framework.
      ServicePointManager.DefaultConnectionLimit = NUM_THREADS

      Dim threads As New List(Of Thread)

      For i As Integer = 0 To NUM_THREADS
        Dim thread As New Thread(AddressOf New KeywordThread(user, i, adGroupId).Run)
        threads.Add(thread)
      Next i

      For i As Integer = 0 To threads.Count - 1
        threads.Item(i).Start(i)
      Next i

      For i As Integer = 0 To threads.Count - 1
        threads.Item(i).Join()
      Next i
    End Sub

    ''' <summary>
    ''' Thread class for validating keywords.
    ''' </summary>
    Public Class KeywordThread
      ''' <summary>
      ''' Index of this thread, for identifying and debugging.
      ''' </summary>
      Dim threadIndex As Integer

      ''' <summary>
      ''' The ad group id to which keywords are added.
      ''' </summary>
      Dim adGroupId As Long

      ''' <summary>
      ''' The AdWords user who owns this ad group.
      ''' </summary>
      Dim user As AdWordsUser

      ''' <summary>
      ''' Number of keywords to be validated in each API call.
      ''' </summary>
      Const NUM_KEYWORDS As Integer = 100

      ''' <summary>
      ''' Initializes a new instance of the <see cref="KeywordThread" /> class.
      ''' </summary>
      ''' <param name="threadIndex">Index of the thread.</param>
      ''' <param name="adGroupId">The ad group id.</param>
      ''' <param name="user">The AdWords user who owns the ad group.</param>
      Public Sub New(ByVal user As AdWordsUser, ByVal threadIndex As Integer, _
          ByVal adGroupId As Long)
        Me.user = user
        Me.threadIndex = threadIndex
        Me.adGroupId = adGroupId
      End Sub

      ''' <summary>
      ''' Main method for the thread.
      ''' </summary>
      ''' <param name="obj">The thread parameter.</param>
      Public Sub Run(ByVal obj As Object)
        ' Create the operations.
        Dim operations As New List(Of AdGroupCriterionOperation)
        For j As Integer = 0 To NUM_KEYWORDS
          ' Create the keyword.
          Dim keyword As New Keyword
          keyword.text = "mars cruise thread " & threadIndex.ToString & " seed " & j.ToString
          keyword.matchType = KeywordMatchType.BROAD

          ' Create the biddable ad group criterion.
          Dim keywordCriterion As AdGroupCriterion = New BiddableAdGroupCriterion
          keywordCriterion.adGroupId = adGroupId
          keywordCriterion.criterion = keyword

          ' Create the operations.
          Dim keywordOperation As New AdGroupCriterionOperation
          keywordOperation.operator = [Operator].ADD
          keywordOperation.operand = keywordCriterion

          operations.Add(keywordOperation)
        Next j

        ' Get the AdGroupCriterionService. This should be done within the
        ' thread, since a service can only handle one outgoing HTTP request
        ' at a time.
        Dim service As AdGroupCriterionService = CType(user.GetService( _
            AdWordsService.v201705.AdGroupCriterionService), AdGroupCriterionService)
        service.RequestHeader.validateOnly = True
        Dim retryCount As Integer = 0
        Const NUM_RETRIES As Integer = 3
        Try
          While (retryCount < NUM_RETRIES)
            Try
              ' Validate the keywords.
              Dim retval As AdGroupCriterionReturnValue = service.mutate(operations.ToArray)
              Exit While
            Catch e As AdWordsApiException
              ' Handle API errors.
              Dim innerException As ApiException = TryCast(e.ApiException, ApiException)
              If (innerException Is Nothing) Then
                Throw New Exception("Failed to retrieve ApiError. See inner exception for more " & _
                    "details.", e)
              End If
              For Each apiError As ApiError In innerException.errors
                If Not TypeOf apiError Is RateExceededError Then
                  ' Rethrow any errors other than RateExceededError.
                  Throw
                End If
                ' Handle rate exceeded errors.
                Dim rateExceededError As RateExceededError = DirectCast(apiError, RateExceededError)
                Console.WriteLine("Got Rate exceeded error - rate name = '{0}', scope = '{1}', " & _
                    "retry After {2} seconds.", rateExceededError.rateScope, _
                    rateExceededError.rateName, rateExceededError.retryAfterSeconds)
                Thread.Sleep(rateExceededError.retryAfterSeconds)
                retryCount = retryCount + 1
              Next
            Finally
              If (retryCount = NUM_RETRIES) Then
                Throw New Exception(String.Format("Could not recover after making {0} attempts.", _
                    retryCount))
              End If
            End Try
          End While
        Catch e As Exception
          Throw New System.ApplicationException("Failed to validate keywords.", e)
        End Try
      End Sub
    End Class
  End Class
End Namespace

Send feedback about...

AdWords API
AdWords API
Need help? Visit our support page.