How to test for a specifc API exception? ...

59 views
Skip to first unread message

BDCoder

unread,
Sep 12, 2014, 1:39:35 PM9/12/14
to adwor...@googlegroups.com
Using .Net API library:

Is there any example that shows how to check for a specific Adwords API exception?

For example, this exception code exists in the library:

Google.Api.Ads.AdWords.v201406.AdGroupAdErrorReason.CANNOT_OPERATE_ON_REMOVED_ADGROUPAD

Now, if I make a call to mutate an adgroup ad that is REMOVED -- How can I check this exception?, ie:

if ( api_return_status = Google.Api.Ads.AdWords.v201406.AdGroupAdErrorReason.CANNOT_OPERATE_ON_REMOVED_ADGROUPAD ) then

   do something

else

   do something else
   
end if

BDCoder

unread,
Sep 12, 2014, 9:45:39 PM9/12/14
to adwor...@googlegroups.com
Below is how I check the error reason code within an AdGroupAdError object returned by the API:

If I am doing something wrong or there is a better (more efficient) way, please reply...

Try

   ret_val = service.mutate( ... )

Catch aw_ex As Google.Api.Ads.AdWords.Lib.AdWordsApiException

'  Get the API exception ...

   Dim api_ex As Google.Api.Ads.AdWords.v201406.ApiException = TryCast( aw_ex.ApiException, Google.Api.Ads.AdWords.v201406.ApiException )

'  Set flag to indicate whether or not the exception was handled ...

   Dim ex_handled = false

   If ( Not api_ex Is Nothing ) Then

      If ( Not api_ex.errors is Nothing ) Then

         If ( api_ex.errors.Length = 1 ) Then

            If ( TypeOf api_ex.errors( 0 ) Is Google.Api.Ads.AdWords.v201406.AdGroupAdError ) then

               Dim aga_error = CType( api_ex.errors( 0 ), Google.Api.Ads.AdWords.v201406.AdGroupAdError )

               If ( aga_error.reason = Google.Api.Ads.AdWords.v201406.AdGroupAdErrorReason.CANNOT_OPERATE_ON_REMOVED_ADGROUPAD ) then

'                                      Do whatever you need set flag to indicate error has been handled ...

                  ex_handled = true

               End If

            End If

         End If

      End If

'     If we could not handle the API error, throw the exception for
'     manual inspection ...

      If ( Not error_handled ) then

         Throw New System.Exception( "AdWords API Error.", aw_ex )

      End If

   End If

Catch ex as System.Exception

'   Handle system exceptions ...

End Try

BDCoder

unread,
Sep 12, 2014, 9:57:27 PM9/12/14
to adwor...@googlegroups.com
Sorry -- previous reply had a logic error -- this works ...

Try

   ret_val = service.mutate( ... )

Catch aw_ex As Google.Api.Ads.AdWords.Lib.AdWordsApiException

'  Get the API exception ...

   Dim api_ex As ApiException = TryCast( aw_ex.ApiException, ApiException )

'  Set flag to indicate whether or not the exception was handled ...

   Dim ex_handled = false

   If ( Not api_ex Is Nothing ) Then

      If ( Not api_ex.errors is Nothing ) Then

         If ( api_ex.errors.Length = 1 ) Then

            Dim aga_error = TryCast( api_ex.errors( 0 ), AdGroupAdError )

            If ( Not aga_error is Nothing )

               If ( aga_error.reason = AdGroupAdErrorReason.CANNOT_OPERATE_ON_REMOVED_ADGROUPAD ) then

'                   Do whatever you need to do ...

                  ex_handled = true

               End If

            End If

         End If

      End If

'     If we could not handle the API error, throw the exception for
'     manual inspection ...

      If ( Not ex_handled ) then

         Throw New System.Exception( "AdWords API Error.", aw_ex )

      End If

   End If

Catch ex As System.Exception

' Handle system Exception ...

End Try
Reply all
Reply to author
Forward
0 new messages