Search code examples
asp-classic

Properties missing from ASP/VBScript error object


This is not the same as the other question because the advice in the other question fails to work for me. I'm specifically trying to determine what's different in my case that this doesn't work.

I'm working in Classic ASP. According to the docs I find online (here and elsewhere: https://www.w3schools.com/ASp/asp_ref_error.asp ), the ASP error object should have the following properties, among others:

  • Description: Returns a short description of the error
  • File: Returns the name of the ASP file that generated the error
  • Line: Returns the line number where the error was detected
  • Number: Returns the standard COM error code for the error

I have the following test code. Note error_log() is a custom function that writes text to a log file.

on error resume next
    blah
    if err.number > 0 then
        error_log( err.number )
        error_log( err.description )
        error_log( err.file )
        error_log( err.line )
    end if
on error goto 0

The first two lines, number and description, are written as expected; but file and line are not. As best I can tell, the latter two properties are simply not available to my copy of ASP/VBScript. Why don't I have these properties?

I'm running what I believe is the latest (circa 2000) version of classic ASP VBScript. Straight from the horse's mouth: VBScript 5.8 build 16384

EDIT: attempts to run Server.GetLastError() fail. It appears I don't have that command for some reason. [CORRECTION: it runs but seems to have no data. See examples below]

New Minimal Example:

On Error Resume Next
    blah    'This is our error. unrecognized variable'
    Set error = Server.GetLastError()
    response.write error.file
On Error Goto 0
response.end

This results in a blank screen. No error data is written.

Another test. Note "error" vs. auto-generated "err":

On Error Resume Next
    blah    'This is our error. unrecognized variable'
    Set error = Server.GetLastError()
    response.write error.number
    response.write " | "
    response.write err.number
    response.write "<br>" & vbCrLf
    response.write error.description
    response.write " | "
    response.write err.description
    response.write "<br>" & vbCrLf
    response.write error.file
    response.write " | "
    response.write error.line
On Error Goto 0
response.end

Result:

0 | 500
| Variable is undefined
| 0

Solution

  • For starters, per user Lankymart, there is a difference between the automatic err object and the object returned by Server.GetLastError(). For this we need the latter of the two.

    It seems that Classic ASP returns a slightly different error code: 500.100; so if you want to capture error data, you have to make an error page explicitly for that sub-code.

    Info found here: https://stackoverflow.com/a/9407559/339440

    Classic ASP has always returned a 500.100 status if there is a script error[...].

    If you want to catch Classic ASP script errors and be able to read the Server.GetLastError() object in your custom error page (say for logging) you need to provide a handler specifically for 500.100.

    If you don't specify a custom 500.100 error then IIS will fall back to your custom (or its own) 500 error page but Server.GetLastError() won't provide any useful information about the error.

    So error referencing "in place" doesn't work, and catching the standard 500 error doesn't work. You need to explicitly catch the 500.100 code. On a regular 500 error, Server.GetLastError() returns an object with no data in it -- which is exactly the problem I've been having.

    So I made a copy of my site's 500 error page, and modified it to also write to my error log. Then in IIS I set that new file to be the error page for 500.100 errors. It appears to be working.

    This doesn't entirely solve my problem, as I was trying to create a sort of try ... catch routine that could run inline. But it does give me the ability to simply log errors, which is very useful.