[英]Classic asp - response.redirect in a sub - error handling
我希望执行以下操作...提供一个开发人员可以重定向的页面,只要发生错误...就像无法打开vb错误连接或找不到对象...或者引发数据库错误。 ..但是因为我将重定向移动到子页面,页面实际上没有重定向...是否有可能我无法从子目录重定向? 看起来很奇怪。
运行引发错误的存储过程
Dim cmd
Set cmd = Server.CreateObject("ADODB.Command")
cmd.ActiveConnection = con
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "spReturnDBException"
cmd.Execute
调用句柄错误函数,设置一些会话参数并在必要时重定向
HandleErrors con _
, Request.ServerVariables("PATH_INFO") _
, "An error occurred while trying to save sessions." _
, "Actual Error: " + Err.Description + " EmpNo: " + Session("EmpNo") _
+ ". QueryString: " + Request.Querystring _
, 0
这将是所谓的子。
sub HandleErrors( connection, WebPagePath, GenericErrorMessage, DebugInfo, Severity)
//Check for vb errors
if Err.Number <> 0 Then
Session("WebPagePath") = WebPagePath
Session("SafeErrorMessage") = GenericErrorMessage 'Session("SafeErrorMessage") + "A connection was dropped while trying to complete sessions."
Session("DebugInfo") = DebugInfo ' Err.Description
Session("LineNo") = Err.Line
Session("StackTrace") = ""
Session("Severity") = Severity
response.redirect("Error.asp")
//error occurs
elseif connection.Errors.count <> 0 then
response.write("a database error occurred.")
// Store safe error message / # in session
Session("WebPagePath") = WebPagePath
Session("SafeErrorMessage") = GenericErrorMessage 'Session("SafeErrorMessage") + "An error has occurred while trying to save sessions."
Session("DebugInfo") = DebugInfo '"Some extra added debug info from the webpage"
Session("LineNo") = 0
Session("StackTrace") = ""
Session("Severity") = Severity
Dim objError
for each objError in connection.Errors
// Store safe error number in session
Session("SafeErrorNumbers") = Session("SafeErrorNumbers") + objError.Description
if connection.Errors.Count > 1 then
Session("SafeErrorNumbers") = Session("SafeErrorNumbers") + "|"
end if
next
response.Redirect("Error.asp")
end if
Err.Clear
end sub
要显示错误行号:
set objError = Server.GetLastError()
strErrorLine = objError.Line
以下是使用Err.line的几个主题:
http://www.daniweb.com/web-development/asp/threads/11615
http://www.sitepoint.com/forums/showthread.php?279612-ASP-Error-Handling.-Err.Line-weird-behavior
我无法解释你为什么会得到你的结果。 我可以告诉你,如果你的代码到达包含Response.Redirect
的行,那么无论是否在sub procdure中都会发生重定向。
我会提出这个建议。 使用On Error Resume Next
停止使用。 处理异常是非常痛苦的方法。
而是将HandleErrors
更改为GenerateConnectionError
。 它的工作是编写错误源和描述字符串,故意用用户定义错误号调用Err.Raise
(我倾向于使用1001)。
现在,您的Error.asp
应作为500.100 http状态代码的处理程序安装在应用程序的根目录中。 发生脚本错误时,IIS将查看当前错误页面集合以确定要执行的操作。 您将设置此项以执行URL并将Error.asp
指定为URL。
执行Error.asp时,它将查找有关从QueryString
请求的页面的详细信息。 在这里,您还可以使用Server.GetLastError
来获取ASPError
对象,您可以从中获取有关错误的其他详细信息。
使用此方法将详细说明任何脚本错误,而无需开发人员记住使用HandleError
代码来HandleError
代码。 开发人员需要记住在执行使用ADODB.Connection的代码时调用GenerateConnectionError
,即使这样你也可能在.asp包含文件中抽象它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.