7.4.4 使用iis错误页面
与asp错误处理过程相关的内容是为iis提供可定制的错误页面。事实上,在iis 4.0中也有这个特点。但新的asp内置对象asperror,更易于使用且提供更加强大的功能。
在第4章,当我们研究server.execute和server.transfer方法时,已经讲述了如何建立定制的错误页面。我们也讨论和使用了asperror对象,但这种方式受到了一定的限制。在这一部分,将介绍如何将定制的错误网页和asperror对象结合起来建立一个更好的处理asp错误的方法。
我们可以使用vbscript检查asperror对象的内容,从而创建一个定制的错误页面。构建一个包含错误内容全面信息的字符串,且写入到服务器磁盘上的日志文件中。然而网页的设计仅使访问者看到网页不可用这样一条信息是不行的,应该使访问者能够选择是重新载入上一个网页还是回到主页,使他们没意识已经发生了错误。
尽管我们采用vbscript创建这个网页,但其使用的一些特性对jscript来说也是适用的,这两种脚本语言的相互转换也是比较容易的。
可以从http://www.wrox.com站点下载本章及本书其他章节的示例文件。
1. 设置定制的错误页面
在能使用定制的错误页面之前,必须在internet services manager进行相应的设置(设置方式见第4章)。把示例文件装入计算机的wwwroot目录中,打开chapter07子目录的properties对话框,在custom errors选项卡中,滚动列表并选中http错误“500:100”条目,点击edit properties按钮,并键入定制的错误页面custom_error.asp的url,如图7-17所示:
图7-17 custom errors选项卡
现在chapter07子目录中的页面出现一个asp错误时,就会打开定制的错误页面。
2. 使用定制的错误页面
在浏览器中打开chapter07目录并选择到“using a custom error page”的链接,这个页面显示了一系列用于产生各种类型的错误的按钮,点击标有“load a page with a syntax error”的按钮,如图7-18所示:
图7-18 演示定制错误页面的屏幕1
这将载入一个名为syntax_error.asp的简单页面。然而看不到这个页面,因为这个页面包含了一个语法错误。asp终止这个页面的编译/执行,并把执行转到定制错误页面,这个页面展示了错误的细节和两个按钮,这两个按钮用以返回上个页面(主菜单)或返回web站点的缺省主页,如图7-19所示:
图7-19 演示定制错误页面的屏幕2
这个页面也把错误报告追加到服务器磁盘c:\temp文件夹中名为custom_error.log的日志文件中,可以在文件编辑器中打开并查看它,图7-20所示的日志文件已经记录了几个错误。
图7-20 日志文件
如果在页面中得到了一个信息,指明日志文件不能写入信息,可能是因为iusr_machinename(iusr_计算机名)帐号没有访问c:\temp目录的权限。当测试这个页面时,应该给予iusr_machinename帐号对这个目录的全部控制权,或者改变custom_error.asp页面的程序代码以指向一个iusr有全部控制权的文件夹
错误消息出现在页面中的唯一原因,是因为在cause_error.asp页面中我们选择了相应的复选框。如果关闭该选项并再次点击按钮,便看不到错误的详细情况,然而错误信息仍然记录在服务器磁盘上的custom_error.log错误日志文件中。
“display debugging information”复选框给定制错误页面(而不是日志文件)提供了更多的信息,有助于调试那些使用asp内置对象集合值的页面,如图7-21所示:
图7-21 cause_error.asp页面的选择框
在本章下面部分,将再讨论这一问题,同时也可以了解“cause an error”页面上的其他按钮所提供的其他种类的错误信息。注意有一些按钮能够比其他的按钮能够提供更多信息。特别是只有最后一个按钮给出asp错误代码的值(这里是asp 0177)。
(1) “cause an error”页面的功能
与先前讨论的示例页面一样,引起错误的页面使用同样的技术,用<form>把值提交给同一个页面。然后asp程序查看窗口上点击的是那个submit按钮,然后运行代码的相应部分。同时查看是否页面上两个复选框是否选中,如果是这样,程序首先设置一个或两个会话级的变量以指明这一点。
<%
see 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
…
%>
由于使用了server.transfer,当错误发生时,正在运行的网页的整个asp环境由iis传给定制错误页面。然而,脚本变量的值并没有传给定制错误页面,所以必须使用session变量,或者把值添加到request.form或request.querystring集合以便把值传送给定制错误页面。
设置了session变量之后,程序继续查看点击了哪个按钮。每个类型的错误(除了第一类型外),都是由运行相应的asp代码产生的,第一类型的错误需要调用另一个页面。
…
look for a command sent from the 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
%>
(2) 定制错误页面的工作
知道了如何创建错误后,让我们来看看定制的错误页面。在前面的章节里已经知道了构建网页需要的理论,这里再概要地描述一下其工作过程。第一步是关闭缺省的错误处理器以便页面程序不被另一个错误中断。第二步通过创建一个新的asperror对象收集原始错误信息。进行这个工作时要格式化一些值,并把它们转换成合适的数据类型。
<%
prevent any other errors from stopping execution
on error resume next
get a reference to the asperror object
set objasperror = server.getlasterror()
get the property values
strerrnumber = cstr(objasperror.number) normal error code
straspcode = objasperror.aspcode asp error code (if available)
if len(straspcode) then
straspcode = "" & straspcode & " "
else
straspcode = ""
end if
strerrdescription = objasperror.description
straspdescription = objasperror.aspdescription
strcategory = objasperror.category type or source of error
strfilename = objasperror.file file path and name
strlinenum = objasperror.line line number in file
strcolnum = objasperror.column column number in line
if isnumeric(strcolnum) then if available convert to integer
lngcolnum = clng(strcolnum)
else
lngcolnum = 0
end if
strsourcecode = objasperror.source source code of line
…
现在构建一个错误报告字符串,这段程序看起来复杂,但实际上仅是一系列if …then语句的嵌套,用以产生良好的报告格式,没有任何空的段落。如果错误是语法错误,来自asperror对象的source属性的源代码可在strsourcecode变量中得到,可以使用这个变量及lngcolnum的值(从asperror对象的column属性中得到)增加一个标记用来指明在源程序中的什么地方发现了错误。
…
create the error message string
strdetail = "asp error " & straspcode & "occurred " & now
if len(strcategory) then
strdetail = strdetail & " in " & strcategory
end if
strdetail = strdetail & vbcrlf & "error number: " & strerrnumber _
& " (0x" & hex(strerrnumber) & ")" & vbcrlf
if len(strfilename) then
strdetail = strdetail & "file: " & strfilename
if strlinenum > "0" then
strdetail = strdetail & ", line " & strlinenum
if lngcolnum > 0 then
strdetail = strdetail & ", column " & lngcolnum
if len(strsourcecode) then
get the source line so put a ^ marker in the string
strdetail = strdetail & vbcrlf & strsourcecode & vbcrlf _
& string(lngcolnum – 1, "-") & "^"
end if
end if
end if
strdetail = strdetail & vbcrlf
end if
strdetail = strdetail & strerrdescription & vbcrlf
if len(straspdescription) then
strdetail = strdetail & "asp reports: " & straspdescription & vbcrlf
end if
…
(3) 记录错误
用名为strdetail的字符串变量创建了错误报告后,可以像在第5章中做的那样,采用filesystemobject对象把它追加到日志文件中。如果成功,布尔型“failed flag”变量将被设置成false。
…
now log error to a file. edit the path to suit your machine.
you need to give the iusr_machinename permission to write and modify
the file or directory used for the log file:
strerrorlog = "c:\temp\custom_error.log"
set objfso = server.createobject("scripting.filesystemobject")
set objtstream = objfso.opentextfile(strerrorlog, 8, true) 8 = forappending
if err.number = 0 then objtstream.writeline strdetail & vbcrlf
if err.number = 0 then
objtstream.close
blnfailedtolog = false
else
blnfailedtolog = true
end if
%>
(4) 跳转到另一个页面
现在准备在网页中创建一些输出。在此之前,需要检查错误细节以确定下一步要做什么。例如,可用asperror对象的number或其他属性检查错误类型。在这里,可认为“type mismatch”错误不是代码中有错误,可能是由于用户在文本框中输入错误数据产生的。所以不显示这个网页的剩余部分,而是跳转到另一个网页
if objasperror.number = -2146828275 then 0x800a000d – type mismatch
response.clear
response.redirect "/" go to the home page
end if
是否决定这样做依赖于你自己的情况以及你打算发现、记录或显示的错误类型。需要注意的是,因为我们不想把目前的网页环境传送到新的网页上,所以选择使用reponse.redirect语句而不是用server.transfer语句。
(5) 显示错误信息
最后,显示错误报告和其他信息以及返回到上一个网页或主页的按钮。
<%
see if the logging to file failed
if so, display message
if blnfailedtolog then
response.write "<b>warning: cannot log error to file " & strerrorlog & "</b>.<p>"
end if
see if we are displaying the error information
if session("showerror") = "yes" then
%>
<pre><% = server.htmlencode(strdetail) %></pre>
<%
end if
see if we are displaying the debug information
if session("showdebug") = "yes" then server.transfer "debug_request.asp"
create the buttons to return to the previous or home page
strreferrer = request.servervariables("http_referer")
if len(strreferrer) then
%>
<form action="<% = strreferrer %>">
<input type="submit" name="cmdok" value=" ">
return to the previous page<p>
</form>
<%
end if
%>
<form action="/">
<input type="submit" name="cmdok" value=" ">
go to our home page<p>
</form>
对上面这段程序需要注意的是:在定制错误页面里,不能使用server.execute方法。如果我们这样做的话,至少程序不能正常工作。当程序把执行转到特定的网页时,程序不会再返回到当前网页,这就是我们使用server.transfer方法载入显示调试信息的网页的原因。这是下一部分要讨论的问题。
7.5 程序调试——发现及处理错误
读完上面内容,读者一定很想创建一个没有错误的asp网页。但你可能会发现网页并不能工作。怎么办,只有进行测试。 在这一部分,首先简要看一下能使调试更容易的一些工具。microsoft script debugger试图把调试支持工具提高到像visual basic、delphi和visual c++等大多数传统编程环境的水平。然而,下面将首先讨论一些更传统的有助于跟踪出现在网页中的错误的技术。
