The code samples below provide examples of common error handling functions using the AdWords API. Client Library.
Handle partial failures
' Copyright 2018 Google LLC ' ' 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.v201809 Namespace Google.Api.Ads.AdWords.Examples.VB.v201809 ''' <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) Using adGroupCriterionService As AdGroupCriterionService = CType( user.GetService( AdWordsService.v201809.AdGroupCriterionService), AdGroupCriterionService) ' Set partial failure mode for the service. adGroupCriterionService.RequestHeader.partialFailure = True Try 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 ' 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 Using End Sub End Class End Namespace
Handle rate exceeded errors
' Copyright 2018 Google LLC ' ' 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 System.Net Imports System.Threading Imports Google.Api.Ads.AdWords.Lib Imports Google.Api.Ads.AdWords.v201809 Namespace Google.Api.Ads.AdWords.Examples.VB.v201809 ''' <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) ' Get the AdGroupCriterionService. This should be done within the ' thread, since a service can only handle one outgoing HTTP request ' at a time. Using service As AdGroupCriterionService = CType( user.GetService( AdWordsService.v201809.AdGroupCriterionService), AdGroupCriterionService) service.RequestHeader.validateOnly = True ' 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 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 Using End Sub End Class End Class End Namespace