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

Lars_WestmanLars_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:
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

  • Lars_WestmanLars_Westman Member Posts: 116
    edited 2016-05-16
    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.....
Sign In or Register to comment.