I'm using web services to do some integration between two companies in NAV2016. It works fine but I would like to improve error handling.
Let's say that the web service does a Customer.GET('XYZ'); If that customer doesn't exist I want to show the standard NAV message like "Customer XYZ does not exist" to the user in the calling company instead of the somewhat less informative "Internal Server Error (500)".
Since I'm on NAV2016 I can use a Try-function to catch an error in System.Net.HttpWebResponse, but the only error I've been able to catch is "Internal Server Error (500)". What I want to do is to read the actual XML that the NAV server returns.
This is an example of an error response shown in .Net Web Service Studio:
ResponseCode: 500 (Internal Server Error)
Content-Length:554
Content-Type:text/xml; charset=utf-8
Date:Mon, 16 May 2016 21:10:14 GMT
Server:Microsoft-HTTPAPI/2.0
WWW-Authenticate:oRswGaADCgEAoxIEEAEAAADGGrXREzka6gAAAAA=
<?xml version="1.0" encoding="utf-16"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<faultcode xmlns:a="urn:microsoft-dynamics-schemas/error">a:Microsoft.Dynamics.Nav.Types.Exceptions.NavCSideRecordNotFoundException</faultcode>
<faultstring xml:lang="en-US">The Cost Code does not exist. Identification fields and values: Code='HELPALL_2'</faultstring>
<detail>
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">The Cost Code does not exist. Identification fields and values: Code='HELPALL_2'</string>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>
What I want to do is to load the XML into a MemoryStream and further to an XML document so I can find the tag "faultstring" to show the user the real message.
This is no problem using MSXML, but I would prefer to use System.Net.HttpWebResponse if possible since that's be easier to deploy.
Any ideas on this?
Comments
The solution is of course to avoid Internal Server Error. This can be done by calling try-functions in the functions exposed as web services and passing a variable with the error message.
Something like this:
The variable ErrorMessage is then returned so the response can loook like something like this:
Since we now has a ResponseCode = 200 there's no error to catch and we can find the node "errorMessage" and show the error in a ERROR(....) to show a meaningful error message to the user and roll back the changes in the calling company.
The Try-functionality in NAV2016 really helps a lot!
I hope my pseudo code makes sense.....
http://www.linkedin.com/in/larswestman