蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 ASPError 9 项只读属性、错误分类与 Server.GetLastError 生命周期离线验证

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-ref-error.py
explore-ref-error.py203 行
"""Offline Classic ASP ASPError object verification lab (IIS ASP absent)."""
from dataclasses import dataclass
import json


@dataclass(frozen=True)
class ASPError:
    """Models the read-only 9-property Classic ASP 3.0 ASPError COM object."""

    ASPCode: str = ""
    ASPDescription: str = ""
    Category: str = ""
    Column: int = -1
    Description: str = ""
    File: str = ""
    Line: int = 0
    Number: int = 0
    Source: str = ""


class ServerErrorSimulator:
    def __init__(self) -> None:
        self._last_error: ASPError | None = None

    def record_error(self, err: ASPError) -> None:
        self._last_error = err

    def get_last_error(self) -> ASPError:
        if self._last_error is None:
            return ASPError()
        return self._last_error

    def clear(self) -> None:
        self._last_error = None


def parse_asp_syntax_error(filename: str, lines: list[str], line_no: int, col_no: int) -> ASPError:
    source_line = lines[line_no - 1]
    return ASPError(
        ASPCode="ASP 0131",
        ASPDescription="Disallowed Parent Path or syntax violation in ASP directive.",
        Category="Active Server Pages",
        Column=col_no,
        Description="Active Server Pages error 'ASP 0131'",
        File=filename,
        Line=line_no,
        Number=-2147467259,
        Source=source_line,
    )


def main() -> None:
    cases = []

    # Case 1: 9 read-only properties contract
    err = ASPError(
        ASPCode="ASP 0126",
        ASPDescription="The include file 'missing.inc' was not found.",
        Category="Active Server Pages",
        Column=5,
        Description="Include file not found",
        File="/demo/index.asp",
        Line=3,
        Number=-2147467259,
        Source="<!-- #include file=\"missing.inc\" -->",
    )
    prop_names = sorted(err.__dataclass_fields__.keys())
    readonly_enforced = False
    try:
        err.Line = 99  # type: ignore[misc]
    except AttributeError:
        readonly_enforced = True

    expected1 = {
        "property_count": 9,
        "properties": [
            "ASPCode", "ASPDescription", "Category", "Column",
            "Description", "File", "Line", "Number", "Source",
        ],
        "readonly_enforced": True,
        "asp_code": "ASP 0126",
    }
    actual1 = {
        "property_count": len(prop_names),
        "properties": prop_names,
        "readonly_enforced": readonly_enforced,
        "asp_code": err.ASPCode,
    }
    cases.append({
        "name": "asperror_nine_readonly_properties",
        "summary": "Verify ASPError exposes all 9 read-only properties and blocks attribute mutation.",
        "expected": expected1,
        "actual": actual1,
        "passed": actual1 == expected1,
    })

    # Case 2: Source file, 1-based line, column, and offending source line capture
    sample_lines = [
        "<%@ Language=VBScript %>",
        "<html><body>",
        "<% Response.Write(1 / 0) %>",
        "</body></html>",
    ]
    loc_err = parse_asp_syntax_error("/app/calc.asp", sample_lines, line_no=3, col_no=21)
    expected2 = {
        "file": "/app/calc.asp",
        "line": 3,
        "column": 21,
        "source": "<% Response.Write(1 / 0) %>",
    }
    actual2 = {
        "file": loc_err.File,
        "line": loc_err.Line,
        "column": loc_err.Column,
        "source": loc_err.Source,
    }
    cases.append({
        "name": "asperror_location_and_source_line",
        "summary": "Verify File, Line, Column, and Source locate the exact failing ASP line.",
        "expected": expected2,
        "actual": actual2,
        "passed": actual2 == expected2,
    })

    # Case 3: Category classification (ASP vs VBScript runtime vs COM object)
    vb_err = ASPError(
        ASPCode="",
        ASPDescription="",
        Category="Microsoft VBScript runtime",
        Column=-1,
        Description="Division by zero",
        File="/app/calc.asp",
        Line=3,
        Number=-2146828277,
        Source="",
    )
    com_err = ASPError(
        ASPCode="",
        ASPDescription="",
        Category="Microsoft OLE DB Provider for SQL Server",
        Column=-1,
        Description="Login failed for user 'sa'.",
        File="/app/db.asp",
        Line=12,
        Number=-2147217843,
        Source="",
    )
    expected3 = {
        "asp_has_aspdesc": True,
        "vb_category": "Microsoft VBScript runtime",
        "vb_has_aspdesc": False,
        "com_number_negative": True,
    }
    actual3 = {
        "asp_has_aspdesc": bool(loc_err.ASPDescription),
        "vb_category": vb_err.Category,
        "vb_has_aspdesc": bool(vb_err.ASPDescription),
        "com_number_negative": com_err.Number < 0,
    }
    cases.append({
        "name": "asperror_category_classification",
        "summary": "Verify Category, ASPDescription, and COM HRESULT Number across ASP, VBScript, and COM errors.",
        "expected": expected3,
        "actual": actual3,
        "passed": actual3 == expected3,
    })

    # Case 4: Server.GetLastError lifecycle in custom 500;100 error handler
    srv = ServerErrorSimulator()
    empty_before = srv.get_last_error()
    srv.record_error(vb_err)
    fetched = srv.get_last_error()
    srv.clear()
    empty_after = srv.get_last_error()
    expected4 = {
        "before_number": 0,
        "captured_description": "Division by zero",
        "captured_line": 3,
        "after_clear_number": 0,
    }
    actual4 = {
        "before_number": empty_before.Number,
        "captured_description": fetched.Description,
        "captured_line": fetched.Line,
        "after_clear_number": empty_after.Number,
    }
    cases.append({
        "name": "asperror_getlasterror_lifecycle",
        "summary": "Verify Server.GetLastError returns empty defaults before/after clear and exact ASPError on failure.",
        "expected": expected4,
        "actual": actual4,
        "passed": actual4 == expected4,
    })

    print(json.dumps({
        "version": "CPython 3.12.14 offline Classic ASP / VBScript verifier (IIS ASP absent)",
        "cases": cases,
    }, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

入口文件:projects/asp-ref-error-lab/explore-ref-error.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-ref-error.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:ASP

课程关联工程包(登记关联36章) · 含当前章节或实例 · mbskycom202612a1425.zip

下载课程关联工程包:ASP

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1441.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习