Get error code from Google AdSense API calls


In this article I explain about Get error code from Google AdSense API calls

Google AdSense APIs allow AdSense members to programmatically interact with AdSense accounts. However, when there is any exception in any AdSense API calls, it throws an Exception and the error code published by Google is wrapped somewhere deep inside the SOAP Exception. The following code snippet shows how to parse the SoapException class and retrieve the Google error code from it.

This function accepts the exception raised by Google AdSense API calls and returns the error code from the SoapException. If there is no valid error code, then it returns -1.


Public Shared Function GetErrorCode(ByVal ex As Exception) As Short
Dim errorCode As Short = -1

If (ex.GetType().FullName = "System.Web.Services.Protocols.SoapException") Then
Dim sx As System.Web.Services.Protocols.SoapException = ex

If (Not sx.Detail Is Nothing AndAlso Not String.IsNullOrEmpty(sx.Detail.InnerXml)) Then
Dim message As String = sx.Detail.InnerXml

message = "" + message + "" ' The XML message in the exception is not a valid XML. It does not have a ROOT node.

Dim doc As New Xml.XmlDocument
doc.LoadXml(message)

If (doc.GetElementsByTagName("ns1:code").Count > 0 AndAlso Not doc.GetElementsByTagName("ns1:code")(0).FirstChild Is Nothing) Then
Short.TryParse(doc.GetElementsByTagName("ns1:code")(0).FirstChild.Value, errorCode)
End If
End If
End If

Return errorCode
End Function


Comments

Author: Gaurav Aroraa03 Aug 2008 Member Level: Gold   Points : 1

Hi!

Its a good one, but I have tried the same, there is no response, Could you please tell the way to test it?



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: