home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2002 February
/
Chip_2002-02_cd1.bin
/
zkuste
/
vbasic
/
Data
/
Utility
/
RegWiz.exe
/
Dev
/
RegWiz
/
Include
/
CGUIErr.cls
next >
Wrap
Text File
|
2000-01-27
|
11KB
|
304 lines
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "CGUIErr"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' Replacement for native Err object. Wraps it up with enhanced Error
' raising capabilities.
' At the top of each module where it is used put a statement:
' Private Err as New CGUIErr
'
' This will then over-ride the VBA Err object.
'
Private Const VALUES_DELIMITER As String = "|"
Private Const VALUES_START As String = "Values="
Private Const DEFAULT_ERRORTEXT As Long = 9999
Option Explicit
'====================================================================
' Start of Standard Err Properties/Methods
'====================================================================
Public Property Get Description() As String
Description = VBA.Err.Description
End Property
Public Property Get HelpContext() As Long
HelpContext = VBA.Err.HelpContext
End Property
Public Property Let HelpContext(ByVal iHelpContext As Long)
VBA.Err.HelpContext = iHelpContext
End Property
Public Property Get Erl() As Long
Erl = VBA.Erl
End Property
Public Property Get HelpFile() As String
HelpFile = VBA.Err.HelpFile
End Property
Public Property Let HelpFile(ByVal iHelpFile As String)
VBA.Err.HelpFile = iHelpFile
End Property
Public Property Get Source() As String
Source = VBA.Err.Source
End Property
Public Property Get Number() As Long
Number = VBA.Err.Number
End Property
Public Property Let Source(ByVal iSource As String)
VBA.Err.Source = iSource
End Property
Public Property Let Description(ByVal iDescription As String)
VBA.Err.Description = iDescription
End Property
Public Property Let Number(ByVal iNumber As Long)
VBA.Err.Number = iNumber
End Property
Public Sub Clear()
VBA.Err.Clear
End Sub
'====================================================================
' Start of Enhanced Err Properties/Methods
'====================================================================
'Description:
' Raises an error using VBA.Err.Raise
' However, it formats the error description to add information that will be used
' both to build a callstack and provide a more friendly user error message.
'
Public Sub Raise(Optional ByVal Number As Variant, Optional ByVal Source As String, _
Optional ByVal Description As String, Optional ByVal HelpFile As String, _
Optional ByVal HelpContext As Long, Optional ByVal ProcName As String)
Dim sNumber As String
Dim sDescription As String
Dim sErl As String
Dim sSource As String
Dim sProcName As String
Dim lNumber As Long
Const ERRTEXT As String = "Err="
'
'Check that optional parameters are supplied and either use them or
'take the VBA.Err values. Expand the texts into more dev friendly text.
'
If IsMissing(Number) Then lNumber = VBA.Err.Number Else lNumber = Number
If Len(Description) = 0 Then sDescription = VBA.Err.Description Else sDescription = Description
If Len(Source) = 0 Then sSource = VBA.Err.Source Else sSource = Source
If Len(ProcName) = 0 Then sProcName = sSource Else sProcName = ProcName
'Translate the error number into something that may be more meaningful
'Assume that anything 'near' vbObjectError is a user defined error
'
sDescription = FormatDescription(lNumber, sDescription, sProcName)
VBA.Err.Raise lNumber, sSource, sDescription, HelpFile, HelpContext
End Sub
'Description:
' An extended version of raise that expects a number, description and a TokenisedList.
Public Sub RaiseEx(ByVal Number As Long, _
ByVal Description As String, ParamArray Values() As Variant)
Dim sValuesList As String
Dim lIndex As Long
'Paranoia check. If there are no values then quit.
If LBound(Values()) > UBound(Values()) Then
sValuesList = vbNullString
Else
'Build an output string which is the array elements separated by the token
sValuesList = vbNullString
For lIndex = LBound(Values()) To UBound(Values())
sValuesList = sValuesList & CStr(Values(lIndex)) & VALUES_DELIMITER
Next lIndex
sValuesList = vbCrLf & "Values=" & Left$(sValuesList, Len(sValuesList) - Len(VALUES_DELIMITER))
End If
Raise Number, Source, Description & sValuesList, HelpFile, HelpContext
End Sub
'Shows the error.
'The number and description is passed around to preserve it.
Public Sub Show(Optional ByVal ProcName As String)
' Handle custom errors using a Resource string, display the rest
Dim sDescription As String
Dim sOriginalDescription As String
Dim sUserText As String
Dim lErrNo As Long
Dim frmErr As frmError
lErrNo = VBA.Err.Number
sOriginalDescription = FormatDescription(lErrNo, Description, ProcName)
sUserText = FormatUserText(lErrNo, sOriginalDescription)
'Show an error form and then destroy it
Set frmErr = New frmError
frmErr.ShowError lErrNo, sUserText, sOriginalDescription
Set frmError = Nothing
End Sub
'Logs the error for those times when there is nowhere to raise an error to
'Logging must be switched on in the app or it will just not log anywhere
Public Sub Log(Optional ProcName As String)
Dim sDescription As String
Dim lNumber As Long
lNumber = VBA.Err.Number
sDescription = "Application=" & App.Title & vbCrLf & _
"Version=" & App.Major & "." & App.Minor & "." & App.Revision & vbCrLf & _
"Path=" & App.Path & vbCrLf & _
"Error Number=" & lNumber & vbCrLf & _
"Error Text Follows" & vbCrLf & vbCrLf & _
FormatDescription(lNumber, Description, ProcName)
App.LogEvent sDescription, vbLogEventTypeError
End Sub
'====================================================================
' Start of Private Methods
'====================================================================
'Description:
' Creates a piece of text based on a resource string, inserts values into it from
' the original description text.
' If it cannot find a resource string it will use the 9999 default string
Private Function FormatUserText(ByVal ErrNo As Long, ByVal Description As String)
Dim sOutText As String
Dim sInText As String
Dim lTokenPtr As Long
Dim lTokenEnd As Long
Dim lValueListPtr As Long
Dim sValue As String
Dim vntValues As Variant
Dim iIndex As Integer
lValueListPtr = InStr(Description, VALUES_START)
'If a value is found then return it otherwise return null
If lValueListPtr > 0 Then
'A list has been found, extract the list
vntValues = Split(Mid$(Description, lValueListPtr + Len(VALUES_START)), VALUES_DELIMITER)
Else
'Force it to be an empty variant array
vntValues = Array()
End If
'Res string may not be there, if it is not it will force an error
On Error Resume Next
If ErrNo < vbObjectError \ 2 Then
sInText = LoadResString(ErrNo - vbObjectError)
Else
sInText = LoadResString(DEFAULT_ERRORTEXT)
End If
On Error GoTo 0
' A space is added on to ease parsing
sInText = sInText & " "
sOutText = vbNullString
'Parse the input text until nothing is left
' replace tokens with their values
Do While Len(sInText) > 0
lTokenPtr = InStr(sInText, "%")
If lTokenPtr > 0 Then
'Find the end of the number
lTokenEnd = lTokenPtr + 1
Do While IsNumeric(Mid$(sInText, lTokenEnd, 1))
lTokenEnd = lTokenEnd + 1
Loop
'Ignore a percentage symbol on its own, parse it if it is %n
If lTokenEnd - lTokenPtr > 1 Then ' it must be a number
'Split returned a zero based array, users prefer 1 based arrays
iIndex = CInt(Mid$(sInText, lTokenPtr + 1, lTokenEnd - lTokenPtr - 1)) - 1
If iIndex < LBound(vntValues) Or iIndex > UBound(vntValues) Then
sValue = vbNullString
Else
sValue = vntValues(iIndex)
End If
'Replace %n with its equivalent value and then move on
sOutText = sOutText & Left$(sInText, lTokenPtr - 1) & sValue
sInText = Mid$(sInText, lTokenEnd)
Else
'It is a percentage symbol on its own
sOutText = sOutText & Left$(sInText, lTokenEnd - 1)
sInText = Mid$(sInText, lTokenEnd)
End If
Else
sOutText = sOutText & sInText
sInText = vbNullString
End If
Loop
FormatUserText = sOutText
End Function
'Formats the description to produce an error stack and useful additional information
Private Function FormatDescription(ByVal ErrNo As Long, ByVal OldDescription As String, ByVal ProcName As String) As String
Dim sNumber As String
Dim sErl As String
Dim sDescription As String
Const ERRTEXT As String = "Err="
'
'Check that optional parameters are supplied and either use them or
'take the VBA.Err values. Expand the texts into more dev friendly text.
'
sDescription = OldDescription
'Translate the error number into something that may be more meaningful
'Assume that anything 'near' vbObjectError is a user defined error
'
If ErrNo > vbObjectError \ 2 Then
sNumber = vbCrLf & ERRTEXT & ErrNo & " "
Else
sNumber = vbCrLf & ERRTEXT & "vbObjectError+" & CStr(ErrNo - vbObjectError) & " "
End If
'Grab the line number if available
If Erl <> 0 Then sErl = " Line=" & CStr(Erl) & " " Else sErl = " "
'Include the error number if it has changed together with the description
If InStr(sDescription, sNumber) > 0 Then _
sNumber = vbNullString _
Else sNumber = sNumber & vbCrLf & "Description="
If Len(ProcName) = 0 Then
FormatDescription = sNumber & sDescription
Else
FormatDescription = ProcName & sErl & vbCrLf & sNumber & sDescription
End If
End Function