KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility method to use as an Error Handler that is Server and Client friendly
PRODUCT: 4D | VERSION: 13.3 | PLATFORM: Mac & Win
Published On: October 2, 2013

The Tech Tip Use a global Error Handler to help diagnose system problem documents the benefits of usig a global error handler in debugging a remote database installation. The utility below is an Error Handler that implements the advice from that Tech Tip and is 4D Server friendly.

If (True)
   If (False)
      Begin SQL
         /*
          ErrorHandler

          Purpose: Write out errors to a log file and
          display an alert if not on the server
         */
      End SQL
   End if
   C_TEXT($MethodName_T)
   $MethodName_T:=Current method name
    //===================== Declare Variables ==================================
    //method_parameters_declarations
    //---------------------------------------------------------------------------
    //method_wide_constants_declarations
   C_LONGINT(Error_L;ErrorLine_L)
   C_TEXT(ErrorMethod_T;Msg_T)
    //---------------------------------------------------------------------------
    //local_variable_declarations
   C_LONGINT($Ndx;$SOA;$RIS;$Params_L;$OS_L)
   C_TEXT($Path_T;$LineEnding_T;$DTG_T)
   C_TIME($Ref_H)
End if
  //====================== Initialize and Setup ================================

  // Copy the 4D system variables in the dialog variables
  // (
ErrorCode_L:=Error
ErrorMethod_T:=Error method
ErrorLine_L:=Error Line
  // )

ARRAY LONGINT(Codes_aL;0)
ARRAY TEXT(IntComp_aT;0)
ARRAY TEXT(Text_aT;0)
GET LAST ERROR STACK(Codes_aL;IntComp_aT;Text_aT)

  //======================== Method Actions ==================================

Msg_T:="Error "+String(ErrorCode_L)+" while executing the method \""\
  +ErrorMethod_T+"\" at line number "+String(ErrorLine_L)

  // Write to a log file for preservation
  //
$Path_T:=Get 4D folder(Logs Folder)+"Error Logs"
If (Test path name($Path_T)#Is a folder)
   CREATE FOLDER($Path_T)
End if

If (OK=1)
   $Path_T:=$Path_T+Folder separator+"Errors Log.txt"
   If (Test path name($Path_T)#Is a document)
      $Ref_H:=Create document($Path_T)
   Else
      $Ref_H:=Append document($Path_T;"TEXT")
   End if

   If (OK=1)
      PLATFORM PROPERTIES($OS_L)
      If ($OS_L=Windows)
         $LineEnding_T:="\r\n"
      Else
         $LineEnding_T:="\r"
      End if

       // Create a ISO date string
       //
      $DTG_T:=String(Current date;ISO Date)
      $DTG_T:=Substring($DTG_T;1;Position("T";$DTG_T))+String(Current time;HH MM SS)

      SEND PACKET($Ref_H;$DTG_T+"\t"+Msg_T+$LineEnding_T)

       // Current user and Current machine might be useful to
       // add to the log entry at this point as well

      For ($Ndx;1;Size of array(Codes_aL))
         $Path_T:="\t"+String(Codes_aL{$Ndx})\
         +"\t"+IntComp_aT{$Ndx}\
         +"\t"+Text_aT{$Ndx}+$LineEnding_T
         SEND PACKET($Ref_H;$Path_T)
      End for

      CLOSE DOCUMENT($Ref_H)
   End if
End if

  // Don't display a dialog on the server
  //
If (Application type#4D Server)
   Msg_T:=Msg_T+"\rPlease report this error information to the developer ASAP."

    // Display the error dialog
    // (
   DebugAlert
    // )
End if