簡體   English   中英

經典的asp - response.redirect在子錯誤處理中

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM