5 4 Error Handling

From LANSA Open Guide

5.4 Error Handling

This sample program relies on the LANSA Open default handlers, but if required, error information can be retrieved using LceGetStatus and LceGetMessages. A simple function to display errors will look like this:

 

Sub DispLceError(iSessionID As Integer, sSource As String)

    ' this function displays LANSA Open errors

    ' Note that it only does anything if LceDisplayErrors is off

    

    Dim i As Integer

    Dim sErrDesc As String, iErrNo As Long

    Dim sMsg As String, nMsgs As Integer

    

    Dim msgBuff As String ' message buffer to contain final message

    

    msgBuff = ""

    ' Get error status

    sErrDesc = String((MESSAGE_LENGTH + 1), Chr$(0))

    '----- Important force VB to reserve space for DLL reply

    Call LceGetStatus(iErrNo, sErrDesc, MESSAGE_LENGTH)

    '--------------------------------------------------

    If iErrNo > 0 Then

        ' prepare header message with error description

        msgBuff = "Error : " & Str "1n" & sSource

        msgBuff = msgBuff & Chr(13) & sErrDesc & Chr(13) & Chr(13)

        ' read remaining messages

        sMsg = String(MESSAGE_LENGTH * 3, Chr(0))

        iRet = LceGetMessageCount(iSessionID, nMsgs)

        '------------------------------------------

        For i = 1 To nMsgs '

            sMsg = String(MESSAGE_LENGTH * 3, Chr(0))

            iRet = LceGetMessage(iSessionID, i, sMsg, MESSAGE_LENGTH)

            '--------------------------------------------------------

            sMsg = sTrim(sMsg) ' sTrim detects null terminated strings

            msgBuff = msgBuff + sMsg ' add message to buffer

        Next

        msgbox msgBuff

    End If

End Sub