The ASP Emporium
Free Active Server Applications and Examples by Bill Gearhart
Online since Friday January 7, 2000

 home > code > tutorials > Create A Centralized Custom ASP Error Page

enter a phrase to search: (advanced search)


 h o m e 

 w h a t 's  n e w 

 a l l   c o d e 
  .net:
    • Fundamentals
    • C# Classes
  classic asp:
    • Code Library
    • ASP Apps
  general:
    • Tutorials
    • SQL

 d o w n l o a d s 

 u s e r   f o r u m s 

 l i n k s 

 s e a r c h 

 s u p p o r t 


Create A Centralized Custom ASP Error Page

Please note, this tutorial does not deal with a 404 or other server errors.
For a tutorial on that, check out the 4 Guys From Rolla article: creating a custom 404 error page.

This system allows you to assign custom errors to common problems that you may experience with your web site and create a centralized error page to display custom errors. This one is so easy, you can implement it into any existing system just by adding a couple lines of code to each script. Here are a couple of examples:

  • example 1: You use an ASP mailer application several times in your web site and you want to track the errors coming from the mailer:

    myMailer.asp
    <%
    Dim Mailer  ' my mailer COM object
    
    ' substitute your mailer, I use w3 Jmail
    Set Mailer = Server.CreateObject("jmail.smtpmail")
    
    ' <--- set up mailer info here --->
    
    ' definitely an essential component of any app:
    On Error Resume Next
    Mailer.Execute  ' here is where we tell the mailer to 
                    ' send the mail - some components use
                    ' Send as the command but either way 
                    ' the same thing happens.
    if Err <> 0 then
       ' oops, we got an error...
       ' first free up variable like a good programmer
      Set Mailer = Nothing
       ' then redirect to our new custom error page.
       ' let's make a custom error queryString that we
       ' will always use to represent a mailer error.
      Response.Redirect "customErr.asp?error=1"
    else
       ' <--- display mailer success message -->
      Set Mailer = Nothing
    end if
    %>
  • example 2: You wrote a form handler that requires all fields to be filled out:

    myFormHandler.asp
    <%
    Dim iBadFormData   ' boolean used to represent incomplete form input
                       ' set to false initially
    
    iBadFormData = False
    
    ' check each form input unless we find an empty field
    For Each Item in Request.Form    ' I'm using the post method.
      if iBadFormData = False then
         ' we have not found a bad input yet so check for one.
        if Request.Form(Item) = "" then
           ' someone has not filled in a field of the form
           ' so lets set boolean to true
          iBadFormData = True
        else
           ' this form input has been filled in so
           ' move to the next input.
          iBadFormData = False
        end if
      end if
    NEXT
    
    if iBadFormData = True then
       ' one field is blank so the operation cannot continue
       ' create custom error # for bad form data
      response.redirect "customErr.asp?error=2"
    else
       ' <--- process form information --->
    end if
    %>
  • last example: you wrote a search program that searches a db for a matching record:

    mySearchResults.asp
    <%
    ' here's where our custom error goes:
    If Rs.BOF then
      ' no records found so redirect to custom error page.
      response.redirect "customErr.asp?error=3"
    End If
    %>

Notice something familiar about the three examples above? They all redirect a client to the page called customErr.asp if there is an error of any kind, be it bad form data or no records, etc... Each error has been defined by adding a queryString called error to customErr.asp

Next we will create the centralized error page that will display all of our custom errors.

Below we will create the centralized error page that will display all of our custom errors. The code for this file is extremely simple:

customErr.asp
<%
Dim strTemp      ' string that holds our new custom error message
Dim iErrorValue  ' error query string
Dim iErrorRef    ' the page that produced the error

 ' here's where we do all the work
 ' get the custom error # from the query string
iErrorValue = request.queryString("error")
 ' get the page that the client was refered from
iErrorRef = request.serverVariables("HTTP_REFERER")

 ' select custom error message based on query string
Select Case iErrorValue
  Case 1
     ' we set this ID to represent a mailer error
    strTemp = "Unfortunately there was a problem " & _
      "and your data was not sent."
  Case 2
     ' this id represents bad form data
    strTemp = "You must fill in all fields to submit this form."
  Case 3
     ' no matching records in a db search
    strTemp = "Your search produced no matching entries.<br>" & _
      "<a href=mySearch.asp>try again</a>"
  ' <--- You can add as many custom error #'s as you 
  '      need to by following the above format       --->

  Case Else
     ' this represents any undefined error - override by declaring
     ' an error query string and handling the Case above.
    strTemp = "An undefined error has occurred"
End Select

 ' up to this point, we haven't used iErrorRef
 ' and I'm not going to but if you wanted to keep 
 ' a record of custom errors, you could
 ' open a db here and add the info. If you did that,
 ' the referer becomes crutial data, especially
 ' if you point more than script to the same
 ' custom error #.

 ' CREATE HTML DISPLAY:
%>

<html>
 <head>
  <title>An error has occurred</title>
 </head>
 <body>
  <center>
  <strong>
  <%=strTemp%>
  </strong>
  <br>
  <br>
  Use your browser's back button to try again.
  </center>
 </body>
</html>

Since we used this custom error page, we can reduce the amount of code in each page that displays your custom errors. Please note that this example doesn't really detail ASP errors. To do that you could put the relevant info into a querystring like this:

someASPpage.asp
<%
On Error Resume Next
' <--- my code --->
if err <> 0 then
  response.redirect "customErr.asp?error=10&errNum=" & _
    err.Number & "&errDesc=" & Server.UrlEncode(err.Description)
   ' error - this is our custom error message
   ' errNum - the error number 
   ' errDesc - the error description
end if
%>

and then fix up customErr.asp to display the other error information below your custom error message.