ASP Error Handling

written by Dave on Wednesday, July 02 2008

Error handlng in ASP is very similar to how you would do this in asp.net. You can read about how to implement this for asp.net here.

Here are the basic steps for implementing asp error handling using a integer check on a querystring value.

 

1. Put the following line at the top of your asp page:

<% On Error Resume Next %>

 

2. Scrub your user input

myID = request.QueryString("myID")
myID = int(ScrubUserInput(myID))

Function ScrubUserInput(userInput)
 if userInput <> "" then

  userInput = replace( userInput, "'", "" ) '' (escape the single quote)
  userInput = replace( userInput, """", "" ) '" (double quote)
  userInput = replace( userInput, ")", "" ) ') (close parenthesis)
  userInput = replace( userInput, "(", "" ) '( (open parenthesis)
  userInput = replace( userInput, ";", "" ) '; (semi-colon)
  userInput = replace( userInput, "-", "" ) '- (dash)
  userInput = replace( userInput, "|", "" ) '| (pipe)
  userInput = replace( userInput, "<", "" ) '<
  userInput = replace( userInput, ">", "" ) '>
  userInput = replace( userInput, "{", "" ) '>
  userInput = replace( userInput, "}", "" ) '>
  
  ScrubUserInput = userInput
 end if
End Function

 

3. Check for any errors caused during the conversion to an integer. This code must sit after the conversion is tried.

If Err.number <> 0 then
  TrapError(Err.description)
End If

 

4. Lastly, here is the code for the TrapError function call.

 

<%
Dim strErrorMessage
Dim bolErrors

'Initialize variables
strErrorMessage = "" 'The error messages for tech. support
bolErrors = False 'Have we found any errors yet?


sub TrapError(strError)
  bolErrors = True 'Error Found

  strErrorMessage = strErrorMessage & strError & ", "
 
  ProcessErrors()
end sub


sub ProcessErrors()
  if bolErrors then
   
 set objError = Server.GetLastError()

  Set errorEMail = CreateObject("CDONTS.NewMail")
      errorEMail.From = "admin@yourdomain.com"
  errorEMail.MailFormat = 0 
  msg = "At " & Now & " the following errors occurred on the page " & Request.ServerVariables("SCRIPT_NAME") & chr(10) & chr(10)
  msg = msg & "Error Message: " & strErrorMessage & chr(10) & chr(10)
  msg = msg & "Error Message: " & Request.ServerVariables("SCRIPT_NAME") & chr(10) & chr(10)
  msg = msg & "Raw Header: " & Request.ServerVariables("ALL_RAW") & chr(10) & chr(10)
  msg = msg & "User Agent: " & Request.ServerVariables("HTTP_USER_AGENT") & chr(10) & chr(10)
  msg = msg & "Referer: " & Request.ServerVariables("HTTP_REFERER") & chr(10) & chr(10)
  msg = msg & "IP: " & Request.ServerVariables("REMOTE_ADDR") & chr(10) & chr(10)
  msg = msg & "Server: " & Request.ServerVariables("SERVER_NAME") & chr(10) & chr(10)
  msg = msg & "URL: " & Request.ServerVariables("URL") & chr(10) & chr(10)


  msg = msg & "Line: " & objError.Line & chr(10) & chr(10)
  msg = msg & "Description: " & objError.ASPDescription & chr(10) & chr(10)
  msg = msg & "Querystring: " & Request.Querystring & chr(10) & chr(10)

 


  errorEMail.Body = msg
  errorEMail.To = "admin@yourdomain.com"  
  errorEMail.Subject = "Site Error " & NOW()
  errorEMail.Send
  Set errorEMail  = Nothing

 Response.Redirect("http://www.yourdomain.com/friendlyerrormessagepage.asp")
  end if
end sub 

%>

 

Similar Posts

  1. Error Handling Within Your Asp.Net Web Application
  2. Sending emails withing your asp.net application
  3. Asp.Net Checkbox Validation

Comments are closed

Options:

Size

Colors