Consolidated ASP Classic MVC framework from best components
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

179 lignes
7.1KB

  1. <%
  2. '=======================================================================================================================
  3. ' Error Handler Library
  4. ' Provides centralized error handling and logging functionality
  5. '=======================================================================================================================
  6. Class ErrorHandler_Class
  7. Private m_log_to_file
  8. Private m_log_file_path
  9. Private Sub Class_Initialize()
  10. m_log_to_file = False
  11. m_log_file_path = ""
  12. End Sub
  13. ' Configure whether to log errors to file
  14. Public Property Let LogToFile(value)
  15. m_log_to_file = value
  16. End Property
  17. Public Property Get LogToFile()
  18. LogToFile = m_log_to_file
  19. End Property
  20. ' Set the log file path
  21. Public Property Let LogFilePath(value)
  22. m_log_file_path = value
  23. End Property
  24. Public Property Get LogFilePath()
  25. LogFilePath = m_log_file_path
  26. End Property
  27. '---------------------------------------------------------------------------------------------------------------------
  28. ' Main error handling method
  29. ' context: String describing where the error occurred
  30. ' errObj: VBScript Err object (optional, uses global Err if not provided)
  31. '---------------------------------------------------------------------------------------------------------------------
  32. Public Sub HandleError(context, errObj)
  33. Dim isDevelopment
  34. isDevelopment = (LCase(GetAppSetting("Environment")) = "development")
  35. If IsEmpty(errObj) Or Not IsObject(errObj) Then
  36. ' Use global Err object if none provided
  37. If Err.Number <> 0 Then
  38. If isDevelopment Then
  39. ShowDetailedError context, Err
  40. Else
  41. ShowGenericError
  42. End If
  43. LogError context, Err
  44. End If
  45. Else
  46. If errObj.Number <> 0 Then
  47. If isDevelopment Then
  48. ShowDetailedError context, errObj
  49. Else
  50. ShowGenericError
  51. End If
  52. LogError context, errObj
  53. End If
  54. End If
  55. End Sub
  56. '---------------------------------------------------------------------------------------------------------------------
  57. ' Display detailed error information (Development mode only)
  58. '---------------------------------------------------------------------------------------------------------------------
  59. Private Sub ShowDetailedError(context, errObj)
  60. Dim errHtml
  61. 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;'>"
  62. errHtml = errHtml & "<strong>Error Occurred</strong><br>"
  63. If Not IsEmpty(context) And Len(context) > 0 Then
  64. errHtml = errHtml & "<strong>Context:</strong> " & Server.HTMLEncode(context) & "<br>"
  65. End If
  66. errHtml = errHtml & "<strong>Time:</strong> " & Now() & "<br>"
  67. errHtml = errHtml & "<strong>Number:</strong> " & errObj.Number & "<br>"
  68. errHtml = errHtml & "<strong>Description:</strong> " & Server.HTMLEncode(errObj.Description) & "<br>"
  69. If Len(errObj.Source) > 0 Then
  70. errHtml = errHtml & "<strong>Source:</strong> " & Server.HTMLEncode(errObj.Source) & "<br>"
  71. End If
  72. errHtml = errHtml & "</div>"
  73. Response.Write errHtml
  74. End Sub
  75. '---------------------------------------------------------------------------------------------------------------------
  76. ' Display generic error message (Production mode)
  77. '---------------------------------------------------------------------------------------------------------------------
  78. Private Sub ShowGenericError()
  79. Dim errHtml
  80. 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;'>"
  81. errHtml = errHtml & "<strong>An error occurred</strong><br>"
  82. errHtml = errHtml & "We apologize for the inconvenience. The error has been logged and will be investigated.<br>"
  83. errHtml = errHtml & "Please try again later or contact support if the problem persists."
  84. errHtml = errHtml & "</div>"
  85. Response.Write errHtml
  86. End Sub
  87. '---------------------------------------------------------------------------------------------------------------------
  88. ' Log error to file (if enabled)
  89. '---------------------------------------------------------------------------------------------------------------------
  90. Private Sub LogError(context, errObj)
  91. If Not m_log_to_file Or Len(m_log_file_path) = 0 Then Exit Sub
  92. On Error Resume Next
  93. Dim fso, logFile, logEntry
  94. Set fso = Server.CreateObject("Scripting.FileSystemObject")
  95. ' Create log entry
  96. logEntry = "[" & Now() & "] "
  97. If Len(context) > 0 Then
  98. logEntry = logEntry & "Context: " & context & " | "
  99. End If
  100. logEntry = logEntry & "Error #" & errObj.Number & ": " & errObj.Description
  101. If Len(errObj.Source) > 0 Then
  102. logEntry = logEntry & " | Source: " & errObj.Source
  103. End If
  104. logEntry = logEntry & vbCrLf
  105. ' Append to log file
  106. If fso.FileExists(m_log_file_path) Then
  107. Set logFile = fso.OpenTextFile(m_log_file_path, 8, False) ' 8 = ForAppending
  108. Else
  109. Set logFile = fso.CreateTextFile(m_log_file_path, True)
  110. End If
  111. logFile.Write logEntry
  112. logFile.Close
  113. Set logFile = Nothing
  114. Set fso = Nothing
  115. On Error GoTo 0
  116. End Sub
  117. '---------------------------------------------------------------------------------------------------------------------
  118. ' Quick check if an error exists
  119. '---------------------------------------------------------------------------------------------------------------------
  120. Public Function HasError()
  121. HasError = (Err.Number <> 0)
  122. End Function
  123. '---------------------------------------------------------------------------------------------------------------------
  124. ' Clear the current error
  125. '---------------------------------------------------------------------------------------------------------------------
  126. Public Sub ClearError()
  127. Err.Clear
  128. End Sub
  129. '---------------------------------------------------------------------------------------------------------------------
  130. ' Check and handle error in one call
  131. '---------------------------------------------------------------------------------------------------------------------
  132. Public Function CheckAndHandle(context)
  133. If Err.Number <> 0 Then
  134. HandleError context, Err
  135. CheckAndHandle = True
  136. Else
  137. CheckAndHandle = False
  138. End If
  139. End Function
  140. End Class
  141. ' Singleton instance
  142. Dim ErrorHandler_Class__Singleton
  143. Function ErrorHandler()
  144. If IsEmpty(ErrorHandler_Class__Singleton) Then
  145. Set ErrorHandler_Class__Singleton = New ErrorHandler_Class
  146. End If
  147. Set ErrorHandler = ErrorHandler_Class__Singleton
  148. End Function
  149. %>

Powered by TurnKey Linux.