<%
'=======================================================================================================================
' Error Handler Library
' Provides centralized error handling and logging functionality
'=======================================================================================================================

Class ErrorHandler_Class
    Private m_log_to_file
    Private m_log_file_path

    Private Sub Class_Initialize()
        m_log_to_file = False
        m_log_file_path = ""
    End Sub

    ' Configure whether to log errors to file
    Public Property Let LogToFile(value)
        m_log_to_file = value
    End Property

    Public Property Get LogToFile()
        LogToFile = m_log_to_file
    End Property

    ' Set the log file path
    Public Property Let LogFilePath(value)
        m_log_file_path = value
    End Property

    Public Property Get LogFilePath()
        LogFilePath = m_log_file_path
    End Property

    '---------------------------------------------------------------------------------------------------------------------
    ' Main error handling method
    ' context: String describing where the error occurred
    ' errObj: VBScript Err object (optional, uses global Err if not provided)
    '---------------------------------------------------------------------------------------------------------------------
    Public Sub HandleError(context, errObj)
        Dim isDevelopment
        isDevelopment = (LCase(GetAppSetting("Environment")) = "development")

        If IsEmpty(errObj) Or Not IsObject(errObj) Then
            ' Use global Err object if none provided
            If Err.Number <> 0 Then
                If isDevelopment Then
                    ShowDetailedError context, Err
                Else
                    ShowGenericError
                End If
                LogError context, Err
            End If
        Else
            If errObj.Number <> 0 Then
                If isDevelopment Then
                    ShowDetailedError context, errObj
                Else
                    ShowGenericError
                End If
                LogError context, errObj
            End If
        End If
    End Sub

    '---------------------------------------------------------------------------------------------------------------------
    ' Display detailed error information (Development mode only)
    '---------------------------------------------------------------------------------------------------------------------
    Private Sub ShowDetailedError(context, errObj)
        Dim errHtml
        errHtml = "<div style='padding:15px; margin:10px; border:2px solid #dc3545; background:#f8d7da; color:#721c24; font-family:Verdana,sans-serif; font-size:14px; border-radius:4px;'>"
        errHtml = errHtml & "<strong>Error Occurred</strong><br>"

        If Not IsEmpty(context) And Len(context) > 0 Then
            errHtml = errHtml & "<strong>Context:</strong> " & Server.HTMLEncode(context) & "<br>"
        End If

        errHtml = errHtml & "<strong>Time:</strong> " & Now() & "<br>"
        errHtml = errHtml & "<strong>Number:</strong> " & errObj.Number & "<br>"
        errHtml = errHtml & "<strong>Description:</strong> " & Server.HTMLEncode(errObj.Description) & "<br>"

        If Len(errObj.Source) > 0 Then
            errHtml = errHtml & "<strong>Source:</strong> " & Server.HTMLEncode(errObj.Source) & "<br>"
        End If

        errHtml = errHtml & "</div>"
        Response.Write errHtml
    End Sub

    '---------------------------------------------------------------------------------------------------------------------
    ' Display generic error message (Production mode)
    '---------------------------------------------------------------------------------------------------------------------
    Private Sub ShowGenericError()
        Dim errHtml
        errHtml = "<div style='padding:15px; margin:10px; border:2px solid #dc3545; background:#f8d7da; color:#721c24; font-family:Verdana,sans-serif; font-size:14px; border-radius:4px;'>"
        errHtml = errHtml & "<strong>An error occurred</strong><br>"
        errHtml = errHtml & "We apologize for the inconvenience. The error has been logged and will be investigated.<br>"
        errHtml = errHtml & "Please try again later or contact support if the problem persists."
        errHtml = errHtml & "</div>"
        Response.Write errHtml
    End Sub

    '---------------------------------------------------------------------------------------------------------------------
    ' Log error to file (if enabled)
    '---------------------------------------------------------------------------------------------------------------------
    Private Sub LogError(context, errObj)
        If Not m_log_to_file Or Len(m_log_file_path) = 0 Then Exit Sub

        On Error Resume Next
        Dim fso, logFile, logEntry

        Set fso = Server.CreateObject("Scripting.FileSystemObject")

        ' Create log entry
        logEntry = "[" & Now() & "] "
        If Len(context) > 0 Then
            logEntry = logEntry & "Context: " & context & " | "
        End If
        logEntry = logEntry & "Error #" & errObj.Number & ": " & errObj.Description

        If Len(errObj.Source) > 0 Then
            logEntry = logEntry & " | Source: " & errObj.Source
        End If

        logEntry = logEntry & vbCrLf

        ' Append to log file
        If fso.FileExists(m_log_file_path) Then
            Set logFile = fso.OpenTextFile(m_log_file_path, 8, False) ' 8 = ForAppending
        Else
            Set logFile = fso.CreateTextFile(m_log_file_path, True)
        End If

        logFile.Write logEntry
        logFile.Close
        Set logFile = Nothing
        Set fso = Nothing

        On Error GoTo 0
    End Sub

    '---------------------------------------------------------------------------------------------------------------------
    ' Quick check if an error exists
    '---------------------------------------------------------------------------------------------------------------------
    Public Function HasError()
        HasError = (Err.Number <> 0)
    End Function

    '---------------------------------------------------------------------------------------------------------------------
    ' Clear the current error
    '---------------------------------------------------------------------------------------------------------------------
    Public Sub ClearError()
        Err.Clear
    End Sub

    '---------------------------------------------------------------------------------------------------------------------
    ' Check and handle error in one call
    '---------------------------------------------------------------------------------------------------------------------
    Public Function CheckAndHandle(context)
        If Err.Number <> 0 Then
            HandleError context, Err
            CheckAndHandle = True
        Else
            CheckAndHandle = False
        End If
    End Function

End Class

' Singleton instance
Dim ErrorHandler_Class__Singleton
Function ErrorHandler()
    If IsEmpty(ErrorHandler_Class__Singleton) Then
        Set ErrorHandler_Class__Singleton = New ErrorHandler_Class
    End If
    Set ErrorHandler = ErrorHandler_Class__Singleton
End Function

%>
