Xero - beautiful accounting software

Xero Developer Help Center

Xero Developer Community

Community > API Endpoints >

ApiException

Started by Brendan Doherty -   in API Endpoints

The schema available at https://github.com/XeroAPI/XeroAPI-Schemas/blob/master/v2.00/ApiException.xsd does not fully describe the xml returned.

<!-- API Exception -->
<xs:complexType name="ApiException">
<xs:sequence>
<xs:element name="ErrorNumber" type="xs:integer" minOccurs="1" maxOccurs="1" />
<xs:element name="Type" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="Message" type="xs:string" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>

Response

<ApiException xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ErrorNumber>10</ErrorNumber>
<Type>ValidationException</Type>
<Message>A validation exception occurred</Message>
<Elements>
<DataContractBase xsi:type="Contact">
<ValidationErrors>
<ValidationError>
<Message>There is an existing active contact using the specified contact name. A contact using the same name cannot be created.</Message>
</ValidationError>
</ValidationErrors>
<Warnings />
<ContactID>00000000-0000-0000-0000-000000000000</ContactID>
<Name>Natalie Doherty3</Name>
<EmailAddress>natalie@doherty.net.nz</EmailAddress>
<Addresses />
<Phones />
<UpdatedDateUTC xsi:nil="true" />
<ContactGroups />
<IsSupplier xsi:nil="true" />
<IsCustomer xsi:nil="true" />
</DataContractBase>
</Elements>
</ApiException>

I'm using JAXB to generate java classes from the xml. Using the supplied xsd I can only get "A validation exception occurred", not the good part of the message that says "There is an existing active contact using the specified contact name. A contact using the same name cannot be created.".

Are you guys able to update the XSD?
Hi Brendan,

The <ApiException> response that you get has a child element called <DataContractBase>, which can actually be any type of document that's PUT/POST'ed to the Xero API. I'm not sure that xml schemas can handle the sort of inheritance that's going on here. In fact, the whole ApiException thing isn't a very nice to have to deal with.

To get around this, we're implemented a simpler response format that should hopefully make developers lives easier. When PUT/POST'ing data to the api, you can add a summarizeErrors=false querystring to the request message. This will supress the ApiException, and instead return the original data that you sent to the API, with any validation messages attached.

See this post for more information:
http://blog.xero.com/developer/api-overview/http-put-and-post/

Dan..
 

Daniel Barratt (Xero Staff)  

That's great. I can now access the errors I expected to get.

List<Contact> contactList = client.putContacts(contacts);
System.out.println(contactList.get(0).getDataContractStatus());
System.out.println(contactList.get(0).getValidationErrors().getValidationError().get(0).getMessage());

ERROR
There is an existing active contact using the specified contact name. A contact using the same name cannot be created.

 

Brendan Doherty  

I am also trying to get the Message (the good part). I have tried to set the summarizeError=false doing this change:

OAuthMessage response = client.invoke(accessor, OAuthMessage.POST, endpointUrl + "Invoices, OAuth.newList("xml", contactsString));

OAuthMessage response = client.invoke(accessor, OAuthMessage.POST, endpointUrl + "Invoices?SummarizeErrors=false", OAuth.newList("xml", contactsString));

When I change this no exception is triggered but I cannot see any inovice with ValidationErrors
How and Where can you get the validation messages?
 

Stefan Tammerl  

When using the summarizeErrors=false option to PUT or POST data, you&#39;ll be returned a list of the objects that you originally sent to the API. Those objects will each have a status attribute denoting if the object was saved OK, was saved and generated one or more WARNINGs or wasn&#39;t saved because of one or more ERRORs.

The returned objects may have a ValidationErrors collection or Warnings collection.

The documentation is a little light and doesn&#39;t currently describe the validation warnings or errors that are returned. I&#39;ll get the team to improve this a little:
http://developer.xero.com/api-overview/http-requests-and-responses/#post-put-creating-many

Dan..
 

Daniel Barratt (Xero Staff)