Easy Error Management - Second Trick: User-Friendly Error Reporting
(Page 3 of 4 )
The second trick involves two steps. The first step is the oh-so-vital “friendly” error message. This is a necessity because, generally speaking, when one sees “Microsoft VBScript runtime error,” there is no obvious plan of action associated with it, leaving both the user and programmer stranded. The second step, admittedly an optional one, is to take the responsibility of reporting the error off of the user, by having the error emailed to you, tracked in a database, or both.
Step 1 - Placate the UserSo, we have to stop users from being scared off by ASP’s inherent, and somewhat cryptic, error messages. This can be accomplished with some soothing color, and a calming explanation of what on earth is wrong. Take a look at the following:
'=========================================================
Sub errorMessage( strMSG )
'=========================================================
dim blOptionalInfo
blOptionalInfo = false
‘=== you can hard-code to true/false, or have it depending on permissions
Response.Write( "<div align=""left"" style=""font: 10pt verdana, arial;margin: 25px;")
Response.Write( "padding:1em;background-color:#E6EAF7; border: 1px solid #BDCFDE"">")
Response.Write( "<h3>You have encountered an error!</h3>" )
Response.Write( strMSG )
If blOptionalInfo Then
Response.Write( "<p><b>Debug Info:</b>
" )
Response.Write( "URL: " & Request.ServerVariables("URL") & "
" )
Response.Write( "QUERY_STRING: " & Request.ServerVariables("QUERY_STRING") & "
" )
Response.Write( "RU: " & Request.ServerVariables("REMOTE_USER") & "
" )
Response.Write( "RA: " & Request.ServerVariables("REMOTE_ADDR") & "
" )
End if
Response.Write( Now() )
Response.Write( "<p><a href=""javascript:history.back()"">Go back</a>
")
Response.Write( "<a href=""/"">Home</a>" )
Response.Write( "</div>" )
Response.End()
End Sub
Explanation: Here we are telling the user in plain English what has just happened, where they are, how they got there, and where to go from here. Here’s an example:
If Not isTrulyNumeric( intUserID ) Then
Call errorMessage("The user id you have supplied is invalid! (“ & intUserID & “)” )
Else
‘proceed
End If
This method of reporting will make your programming life much easier and your users happier, guaranteed or your money back!
Step 2 - Obtaining OmniscienceImagine you were the one contacting a user about an error they encountered, not vice versa. Imagine that not only were you able to explain the error quickly, but also explain that you’ve already fixed it! Imagine the enormous amounts of trust and respect (not to mention $$) that would be heaped upon you!
This is fairly easy to accomplish. All you need to do, is create another subprocedure for emailing/logging the error, and call it from the errorMessage() subprocedure. You’ll want to modify the code below with the all variants you wish to be tracked.
'=========================================================
Sub reportError( strMSG )
'=========================================================
dim arVars, objMail, strBody, i
redim arVars( # of items to track )
arVars( 0 ) = “error:” & strMSG
arVars( 1 ) = “time:” & now()
arVars( 2 ) = “user id:” & intUserID
arVars( 3 ) = “page:” & Request.ServerVariables("URL")
arVars( 4 ) = “querystring:” & Request.QueryString()
arVars( 5 ) = “form data:” & Request.Form()
arVars( # ) = etc…
set objMail = Server.CreateObject(“CDONTS.NewMail”)
objMail.To = “youremail@domain.com”
objMail.Subject = “error report”
strBody = “the following error was encountered” & vbCrLf & vbCrLf
For i = 0 To UBound( arVars )
strBody = strBody & arVars( i ) & vbCrLf
Next
objMail.Body = strBody
objMail.Send()
Set objMail = Nothing
arVars = Null
‘followed by optional tracking in a database
End Sub
So there you have it! With good planning, you can be made aware of 99% of all the errors that occur within your application, without depending on the user to notify you. Lastly, let’s just look at one handy trick to save time with our debugging.
Next: Third Trick: The Quick Debugger >>
More ASP Articles
More By Justin Cook