Classic ASP - Catching 500 Errors

2019-02-13 05:54发布

问题:

I'm trying to diagnose a problem with a site that seems to be throwing an error in the code somewhere. From the error logs it seems be an SQL syntax error caused by bad concatenation of an SQL query with bad code. My problem is, I can't reproduce the error but customers are still getting it and it could be cause by a number of queries. So my plan is to create my own 500 error page to catch the result.

I want to get the page to catch all session data, all POST and GET data (which I can do) but I also want to catch detailed information about the error. Pretty much what would show on the page when the site allows errors to be shown. With the little arrow specifying the line.

Is there a way to catch the error from the custom 500 error page?

Thanks in advance

Grant

回答1:

You can get good, but not great info from ASP when you have an error.

But you can define a custom 500 error code page in ASP land that can give you a bit more info when your program crashes. Here's some sample code that will build a pretty decent error message about your error.

  Set objASPError = Server.GetLastError

  Dim strProblem
  strProblem = "ASPCode: " & Server.HTMLEncode(objASPError.ASPCode) & vbCrLf
  strProblem = strProblem & "Number: 0x" & Hex(objASPError.Number) & vbCrLf
  strProblem = strProblem & "Source: [" & Server.HTMLEncode(objASPError.Source) & "]" & vbCrLf
  strProblem = strProblem & "Category: " & Server.HTMLEncode(objASPError.Category) & vbCrLf
  strProblem = strProblem & "File: " & Server.HTMLEncode(objASPError.File) & vbCrLf
  strProblem = strProblem & "Line: " & CStr(objASPError.Line) & vbCrLf
  strProblem = strProblem & "Column: " & CStr(objASPError.Column) & vbCrLf
  strProblem = strProblem & "Description: " & Server.HTMLEncode(objASPError.Description) & vbCrLf
  strProblem = strProblem & "ASP Description: " & Server.HTMLEncode(objASPError.ASPDescription) & vbCrLf
  strProblem = strProblem & "Server Variables: " & vbCrLf & Server.HTMLEncode(Request.ServerVariables("ALL_HTTP")) & vbCrLf
  strProblem = strProblem & "QueryString: " & Server.HTMLEncode(Request.QueryString) & vbCrLf
  strProblem = strProblem & "URL: " & Server.HTMLEncode(Request.ServerVariables("URL")) & vbCrLf
  strProblem = strProblem & "Content Type: " & Server.HTMLEncode(Request.ServerVariables("CONTENT_TYPE")) & vbCrLf
  strProblem = strProblem & "Content Length: " & Server.HTMLEncode(Request.ServerVariables("CONTENT_LENGTH")) & vbCrLf
  strProblem = strProblem & "Local Addr: " & Server.HTMLEncode(Request.ServerVariables("LOCAL_ADDR")) & vbCrLf
  strProblem = strProblem & "Remote Addr: " & Server.HTMLEncode(Request.ServerVariables("LOCAL_ADDR")) & vbCrLf
  strProblem = strProblem & "Time: " & Now & vbCrLf

Edit On IIS7 GetLastError doesn't seem to have any info available.
You can workaround the problem by creating a 500.100 and point this at your script.
YMMV, check these URLS for more info http://forums.iis.net/t/1150502.aspx and http://www.tacticaltechnique.com/web-development/classic-asp-getlasterror-in-iis7/



回答2:

Generally you set "on error resume next", then check for the error code "Err.Number" after the offending line of code for non-zero values.

See: http://www.powerasp.com/content/new/on-error-resume-next.asp