1

This is a project I inherited. I noticed that sometimes it doesn't log errors.

This routine raises an error so that it can log it along with the rest of the message.
But it never returns to the error point and actually logs the error.
I can see what it is supposed to do, but I can't see how to get it to work.

Public Sub PostErrorToLog(lngErrID As Long, strContext As String, Optional strEvent As String)
    On Error GoTo ErrHandler:

    Dim strErrEvent As String
    Dim rst As New ADODB.Recordset
    Dim pass As Integer
    pass = 0
    
    'Capture event description by triggering the error.
    Err.Raise lngErrID  ' It gets to here, then jumps
    'Use AddNew because SQL may be added into strEvent, causing objSQL.RunADO() to fail.
        Set rst = objSQL.GetRST("PostErrorToLog()", "[System Log]", , , adCmdTable)
            rst.AddNew  ' Never gets here.
                rst![UID] = strCurrUID
                rst![ErrorID] = lngErrID
                rst![Source] = strContext
                rst![Event] = ConcatenateStrings(strErrEvent, strEvent, " ")
            rst.Update
        rst.Close
        FlushLog "Error"
    Exit Sub

ErrHandler:
    strErrEvent = Err.Description
    pass = pass + 1
    If pass > 2 Then   ' Seems like pass is always going to be 1
       Resume Next
    End If  ' It gets to here and exits the routine.
End Sub
GSerg
  • 76,472
  • 17
  • 159
  • 346
BWhite
  • 713
  • 1
  • 7
  • 24
  • Give it a few tries, write `pass = 2` on the 7th line and see what happens. Consider writing `Err.Clear` before `Resume Next`. But in general, if there is no documentation of inherited project, go ahead and rewrite it. You have the allowance of a random person on the internet :D – Vityata Mar 04 '21 at 21:58
  • 2
    `sometimes it doesn't log errors` - this never logs any errors. Maybe it once did, but after some changes it doesn't anymore. From the comment "Use AddNew because SQL may be added into strEvent, causing objSQL.RunADO() to fail" it would also seem that the author of this code was not aware of [parametrized queries](https://stackoverflow.com/q/332365/11683) so chose a creative solution. – GSerg Mar 04 '21 at 22:00
  • @GSerg Parameters: Given the rest of the code, that seems like a safe bet. – BWhite Mar 04 '21 at 22:22
  • @GSerg Never Logs: That's what it look like to me too, but I have entries in the log from this week with error numbers. This is the only routine that adds error numbers, so... – BWhite Mar 04 '21 at 22:23
  • ... so it was changed this week. – GSerg Mar 04 '21 at 22:24
  • The error handler **must** be in same sub or function, see help. – user14797724 Mar 04 '21 at 23:09
  • Errors can be allowed to bubble up. – QHarr Mar 04 '21 at 23:52
  • Review http://allenbrowne.com/ser-23a.html. – June7 Mar 05 '21 at 00:53
  • Thanks all for the comments. I assumed that I was missing something since I couldn't see how it could work. But sounds like not. I'll just rewrite it. – BWhite Mar 06 '21 at 01:41

0 Answers0