|
|
|
|
|
home > code > tutorials > Create A Centralized Custom ASP Error Page |
|
Create A Centralized Custom ASP Error Page
Please note, this tutorial does not deal with a 404
or other server errors. 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:
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. |