简体   繁体   English

经典的asp - response.redirect在子错误处理中

[英]Classic asp - response.redirect in a sub - error handling

I wish to do the following ... Provide a page a developer can redirect to provided an error occurs ... like a vb error connection couldn't open or object couldn't be found ... or a database error is raised ... but since I moved the redirect into a sub the page doesn't actually redirect ... Is it possible that I simply can't redirect from a sub? 我希望执行以下操作...提供一个开发人员可以重定向的页面,只要发生错误...就像无法打开vb错误连接或找不到对象...或者引发数据库错误。 ..但是因为我将重定向移动到子页面,页面实际上没有重定向...是否有可能我无法从子目录重定向? Seems weird. 看起来很奇怪。

Run a stored procedure that raises an error 运行引发错误的存储过程

Dim cmd
Set cmd = Server.CreateObject("ADODB.Command")
cmd.ActiveConnection = con
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "spReturnDBException"
cmd.Execute

Call a handle errors function that sets up some session parms and redirects if neccessary 调用句柄错误函数,设置一些会话参数并在必要时重定向

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

This would be the sub called. 这将是所谓的子。

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

To display the Error line number: 要显示错误行号:

set objError = Server.GetLastError()
strErrorLine =  objError.Line

Here are a couple threads on using Err.line: 以下是使用Err.line的几个主题:

http://www.daniweb.com/web-development/asp/threads/11615 http://www.daniweb.com/web-development/asp/threads/11615

http://www.sitepoint.com/forums/showthread.php?279612-ASP-Error-Handling.-Err.Line-weird-behavior http://www.sitepoint.com/forums/showthread.php?279612-ASP-Error-Handling.-Err.Line-weird-behavior

I can't explain why you are getting the results you are. 我无法解释你为什么会得到你的结果。 I can tell you that if your code reachs a line that contains Response.Redirect that redirect will happen regardless of whether its a in a Sub procdure or not. 我可以告诉你,如果你的代码到达包含Response.Redirect的行,那么无论是否在sub procdure中都会发生重定向。

I would make this suggestion. 我会提出这个建议。 Stop using using On Error Resume Next . 使用On Error Resume Next停止使用。 It is very painful way to deal with exceptions. 处理异常是非常痛苦的方法。

Instead change HandleErrors into GenerateConnectionError . 而是将HandleErrors更改为GenerateConnectionError Its job would be to compose error source and description strings and deliberately call Err.Raise with a user define error number (I tend to use 1001). 它的工作是编写错误源和描述字符串,故意用用户定义错误号调用Err.Raise (我倾向于使用1001)。

Now your Error.asp should be installed in the root of your application as the handler for the 500.100 http status code. 现在,您的Error.asp应作为500.100 http状态代码的处理程序安装在应用程序的根目录中。 When a script error occurs IIS will look to the current error pages collection for the location to determine what to do. 发生脚本错误时,IIS将查看当前错误页面集合以确定要执行的操作。 You will have set this to execute a URL and specified your Error.asp as the URL. 您将设置此项以执行URL并将Error.asp指定为URL。

When Error.asp is executed it will find details about the page being requested from QueryString . 执行Error.asp时,它将查找有关从QueryString请求的页面的详细信息。 Here you can also use Server.GetLastError to get an ASPError object from which you can get other details about the error. 在这里,您还可以使用Server.GetLastError来获取ASPError对象,您可以从中获取有关错误的其他详细信息。

Using this approach will detail with any script error without requiring the developer to remember to pepper their code with HandleError code. 使用此方法将详细说明任何脚本错误,而无需开发人员记住使用HandleError代码来HandleError代码。 The developer need on remember to call GenerateConnectionError when performing code that is using an ADODB.Connection and even then you could probably abstract that inside an .asp include file. 开发人员需要记住在执行使用ADODB.Connection的代码时调用GenerateConnectionError ,即使这样你也可能在.asp包含文件中抽象它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM