Catch the NAV error message in System.Net.HttpWebResponse when talking to a NAV Web Service....

Lars_Westman
Member Posts: 116
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:
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?
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?
Lars Westman
http://www.linkedin.com/in/larswestman
http://www.linkedin.com/in/larswestman
0
Comments
-
Went for a walk with the dog and found the solution....
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:WS_CreateCost(SalesOrderNo : Code[20];VAR SalesOrderLineNo : Integer;CostCode : Code[20];Quantity : Decimal;VAR UnitPrice : Decimal;VAR ErrorMessage : Text) IF NOT TryCreateCost(SalesOrderNo, SalesOrderLineNo, CostCode, Quantity, UnitPrice) THEN ErrorMessage := GETLASTERRORTEXT; LOCAL [TryFunction] TryCreateCost(SalesOrderNo : Code[20];VAR SalesOrderLineNo : Integer;CostCode : Code[20];Quantity : Decimal;VAR UnitPrice : Decimal) ... ... SalesLine.INSERT;
The variable ErrorMessage is then returned so the response can loook like something like this:ResponseCode: 200 (OK) Content-Length:390 Content-Type:text/xml; charset=utf-8 Date:Mon, 16 May 2016 22:58:28 GMT Server:Microsoft-HTTPAPI/2.0 WWW-Authenticate:oRswGaADCgEAoxIEEAEAAACdQOiXKfKDyAAAAAA= <?xml version="1.0" encoding="utf-16"?> <Soap:Envelope xmlns:Soap="http://schemas.xmlsoap.org/soap/envelope/"> <Soap:Body> <WS_CreateCost_Result xmlns="urn:microsoft-dynamics-schemas/codeunit/LogTradeIntercompany"> <salesOrderLineNo>0</salesOrderLineNo> <unitPrice>0</unitPrice> <errorMessage>The Cost Code does not exist. Identification fields and values: Code='ASASD'</errorMessage> </WS_CreateCost_Result> </Soap:Body> </Soap:Envelope>
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.....Lars Westman
http://www.linkedin.com/in/larswestman0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions