Programming | Advanced 7.4.4 using IIS error pages
The content associated with the ASP error handling process is to provide a customizable error page for IIS. In fact, this feature is also available in IIS 4.0. But the new ASP built-in objects are ASPError, easier to use and provide more powerful functionality.
In the 4th chapter, when we look at the Server.Execute and Server.Transfer methods, we've talked about how to create custom error pages. We have also discussed and used the ASPError object, but this approach has been limited in some way. In this section, you'll learn how to combine custom error pages and ASPError objects to create a better way to handle ASP errors.
We can use VBScript to check the contents of the ASPError object to create a custom error page. Constructs a string that contains comprehensive information about the error content and is written to a log file on the server disk. However, the design of the Web page only allows visitors to see the page is not available such a message is not good, should enable visitors to choose whether to reload the previous page or back to the homepage, so that they are not aware that there has been an error.
Although we create this page in VBScript, some of the features used in it are also applicable to JScript, and the translation of the two scripting languages is also relatively easy.
Sample files for this chapter and other chapters of this book can be downloaded from the http://www.wrox.com site.
1. Set up a custom error page
Before you can use a custom error page, you must set up the appropriate settings in Internet Services Manager (see chap. 4th). Load the sample file into the computer's Wwwroot directory, open the Properties dialog box for the CHAPTER07 subdirectory, scroll through the list in the Custom Errors tab, and select the HTTP Error "500:100" entry, click Edit Properties button and type the URL of the custom error page custom_error.asp, as shown in Figure 7-17:
Figure 7-17 Custom Errors tab
Now that the page in the CHAPTER07 subdirectory has an ASP error, the custom error page opens.
2. Use a custom error page
Open the Chapter07 directory in the browser and select the link to "Using a Custom error page", which shows a series of buttons for generating various types of errors, clicking the button labeled "Load a Page with a Syntax error", As shown in Figure 7-18:
Figure 7-18 showing screen 1 for custom error pages
This will load a simple page called syntax_error.asp. However, this page is not visible because the page contains a syntax error. ASP terminates the compilation/execution of this page and transfers execution to the custom error page, which displays the wrong details and two buttons that return to the previous page (main menu) or return to the default home page of the Web site, as shown in Figure 7-19:
Figure 7-19 showing screen 2 for custom error pages
This page also appends the error report to the log file named Custom_error.log in the server disk C:\temp folder, which can be opened and viewed in the file editor, and several errors have been recorded in the log file shown in Figure 7-20.
Figure 7-20 the Japanese records file
If you get a message on the page that the log file cannot write information, it may be because the IUSR_machinename (IUSR_ computer name) account does not have permission to access the C:\temp directory. When testing this page, you should give the IUSR_machinename account full control of the directory, or change the custom_error.asp page's program code to point to a iusr with full control of the folder
The only reason the error message appears on the page is because in the cause_error.asp page we select the appropriate check box. If you turn off this option and click the button again, you cannot see the details of the error, but the error message is still recorded in the Custom_error.log error log file on the server disk.
The Display Debugging information check box provides more information for custom error pages (instead of log files) to help debug pages that use ASP's built-in object collection values, as shown in Figure 7-21:
Figure 7-21 cause_error.asp page selection box
This issue is discussed in the following sections of this chapter, as well as other kinds of error messages that are provided by other buttons on the cause an error page. Note that there are some buttons that can provide more information than other buttons. In particular, only the last button gives the value of the ASP error code (this is ASP 0177).
(1) The function of "cause an Error" page
As with the example page discussed earlier, the page that caused the error uses the same technique, <Form> submits the value to the same page. The ASP program then looks at the submit button on the window and then runs the corresponding section of the code. Also see whether two check boxes are selected on the page, and if so, the program first sets one or two session-level variables to indicate this.
<%
' If we are displaying error and debug information
' Set session variables to retrieve in the custom error page
If Len (Request.Form ("Chkshowerror")) Then
Session ("ShowError") = "yes"
Else
Session ("ShowError") = ""
End If
If Len (Request.Form ("Chkshowdebug")) Then
Session ("showdebug") = "yes"
Else
Session ("showdebug") = ""
End If
...
%>
Because of the use of Server.Transfer, when an error occurs, the entire ASP environment of the running Web page is passed to the custom error page by IIS. However, the value of the script variable is not passed to the custom error page, so you must either use the session variable or add the value to the Request.Form or Request.QueryString collection to pass the value to the custom error page.
After the session variable is set, the program continues to see which button was clicked. Each type of error (except for the first type) is generated by running the corresponding ASP code, and the first type of error requires invoking another page.
...
' Look for a command sent ' FORM section buttons
If Len (Request.Form ("Cmdsyntax")) Then
Response.Clear
Response.Redirect "Syntax_error.asp"
End If
If Len (Request.Form ("Cmdparamtype")) Then
Intdate = "Error"
Intday = Day (intdate)
End If
If Len (Request.Form ("Cmdarray")) Then
Dim Arrthis (3)
Arrthis (4) = "Causes an error"
End If
If Len (Request.Form ("Cmdfile")) Then
Set objFSO = Server.CreateObject ("Scripting.FileSystemObject")
Set Objtstream = objFSO.OpenTextFile ("Does_not_exist.txt")
End If
If Len (Request.Form ("Cmdpagecount")) Then
Set Objpagecount = Server.CreateObject ("MSWC. PageCounter ")
Objpagecount.wrongproperty = 10
End If
If Len (Request.Form ("Cmdobject")) Then
Set objthis = Server.CreateObject ("Doesnot.exist")
End If
[1] [2] [3] Next page