You need to sign in to do that
Don't have an account?
ApiFault object in C#
language: C#
sforce API version 2.5
I am trying to code for rate-limiting by catching fault code 1010 (as per the API Developer's Guide). The Developer's Guide indicates that if an exception occurs on an API call, an exception is thrown and an ApiFault is returned. The ApiFault can be inspected to determine the fault code (ApiFault.exceptionCode).
I don't understand how to retrieve the ApiFault object, e.g. in a typical try/catch block:
try { ...some API } catch (Exception e) { ...handle the exception }
the exception is from C# and does not have the exceptionCode member. Would I cast the exception as an ApiFault ?. Or is the sObject returned by the API call to be cast as an ApiFault? By way of experiment, I tried to declare something as an ApiFault (ApiFault x and the compiler complains that ApiFault is not in the namespace. I checked the WSDL and ApiFault is defined. Clearly I am not understanding how to get at the ApiFault.
Would somebody be so kind as to clarify this for me?
Thanks,
TimC
Hi timc@xegy,
.Net does the error handling differently than java, which was the model for the error handling. In java you get an enum of exception codes that you can then use for comparison. .Net does not create this enum (although I have not tried it using the command line proxy generator).
You can drill into the fault returned with a catch block like the one shown below:
catch (System.Web.Services.Protocols.SoapException ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message);
System.Xml.XmlNode exDetail = ex.Detail;
for (int i=0;i {
if (exDetail.ChildNodes[i].Name.IndexOf("fault") != -1)
{
System.Xml.XmlNode faultNode = exDetail.ChildNodes[i];
foreach (System.Xml.XmlNode fNode in faultNode.ChildNodes)
{
if (fNode.Name.ToLower().IndexOf("code") != -1)
System.Diagnostics.Trace.WriteLine("Exception code is: " + fNode.InnerText);
if (fNode.Name.ToLower().IndexOf("message") != -1)
System.Diagnostics.Trace.WriteLine("Exception message is: " + fNode.InnerText);
}
}
}
}
Dave,
Thank you. That did the trick.
Tim
p.s. sorry about the triple duplication of the posting. I have no idea how that happened and I see no way to remove the extra postings.
I just tried the same thing with Visual Studio 2005 and it didn't work either. Below is the output for when I did this for the v7 WSDL file. At least now I know what the issue is, thanks.
Creating web service proxy file for sforce ...
Schema validation error: Schema item 'simpleType' named 'FaultCode' from namespace 'urn:fault.partner.soap.sforce.com' is invalid. The Enumeration constraining facet is invalid - 'fns' is an undeclared namespace.
Schema validation error: Schema item 'simpleType' named 'FaultCode' from namespace 'urn:fault.partner.soap.sforce.com' is invalid. The Enumeration constraining facet is invalid - 'fns' is an undeclared namespace.
Warning: Schema could not be validated. Class generation may fail or may produce incorrect results.