Search code examples
c#axaptadynamics-ax-2012dynamics-ax-2012-r3

AX 2012 R3 Visual Studio project C# parsing errors


I am writing a wrapper for SSHnet dll from renci (https://sshnet.codeplex.com/), so I can fetch files from a sFTP server via Dynamics AX periodically.

When I catch errors in C# project for example in a try catch statement how can I parse the error message back to AX?

Options I thought of;

  1. Should I put the error message in a string variable and read the string in Dynamics AX?

  2. Write errors to the event log on the AOS / client?


Solution

  • The easiest way to pass error to AX is a rethrow it in C#

    catch (Exception ex) 
    {
        // Do special cleanup, logging etc.
        throw;
    }
    

    and catch it in AX

    catch (Exception::CLRError)
    {
         ex = ClrInterop::getLastException();
         if (ex != null)
         {
            ex = ex.get_InnerException();
            if (ex != null)
            {
                error(ex.ToString());
            }
        }
    }