简体   繁体   中英

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? 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:

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

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.

I would make this suggestion. Stop using using On Error Resume Next . It is very painful way to deal with exceptions.

Instead change HandleErrors into 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).

Now your Error.asp should be installed in the root of your application as the handler for the 500.100 http status code. When a script error occurs IIS will look to the current error pages collection for the location to determine what to do. You will have set this to execute a URL and specified your Error.asp as the URL.

When Error.asp is executed it will find details about the page being requested from QueryString . Here you can also use Server.GetLastError to get an ASPError object from which you can get other details about the error.

Using this approach will detail with any script error without requiring the developer to remember to pepper their code with HandleError code. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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