APGen Documentation Previous Topic: Accessing ASP Objects in APG Scripts Next Topic: Executing APG Scripts in SQL Server Triggers Parent Topic: Executing APG Scripts in Active Server Pages    Executing APG Scripts in Active Server Pages
APG Script Status Reporting in ASP
See Also:

When running APG scripts in ASP pages, it is common to report the status of APG script execution, including error reporting.  This topic shows how.

Example

This example code uses the APGScript object and APGScript.OnError to run APG scripts and monitor them.  The scripts that are run are reported, the files that are generated are reported, and any errors that occur are reported.  This code is adapted from the build.asp page in the Adventure Works Equipment example, found at Program Files\APGen\Examples\awe_apg\build.asp.

This page, build.asp, executes an array of APG scripts and reports the status.  In the Adventure Works Equipment example, the "Anonymous Access" web permission is removed from this page.  This makes it so only authenticated users can execute this page, and the files will only be successfully generated if the user has file write permissions in the output directory.  This design is effective for implementing an "administrators only" build page.

<%
' Build script build.asp
' This script acts as a makefile, building all the ASP files
' from the APG scripts in the /apg directory.

Option Explicit

'****************************************************************************
' PROCEDURE:    APGErrorHandler
' PURPOSE:        If there's an error in an APG script, output the info
'                to the ASP Response object
'****************************************************************************

' Counts how many APG script errors have occurred
Dim nErrorCount
nErrorCount = 0

Sub APGErrorHandler(oAPGError)
    ' Write out an error message with all the error info
%></TR><TR><TD colspan=2>
<H4 style='color:red'>An error has occurred while running APG script "
<% =oAPGError.Script.Path %>".</H4>
                    
<TABLE width='80%' align='center'>
    <COLGROUP><COL valign='top' style='font-weight:bold' />
    <COL valign='top' /></COLGROUP>
    <TR><TD>Error Number</TD>    <TD>0x
<% =Hex(oAPGError.Number) %></TD></TR>
    <TR><TD>Description</TD>    <TD>
<% =oAPGError.Description %></TD></TR>
    <TR><TD>Script</TD>            <TD>
<% =oAPGError.FilePath %></TD></TR>
    <TR><TD>Line Number</TD>    <TD>
<% =oAPGError.LineNumber %></TD></TR>
    <TR><TD>Error Source</TD>    <TD>
<% =oAPGError.Source %></TD></TR>
    <TR><TD>Output File</TD>    <TD>
<% =oAPGError.Script.Output.Path %></TD></TR>
</TABLE><P />

</TD></TR>
<%    ' Send this error info to the client right away
    Response.Flush

    ' Increment the Error Count
    nErrorCount = nErrorCount + 1

    ' Mark the APG error as handled.
    ' This prevents the default error handler from being called.
    ' To debug when errors occur, comment out this line
    oAPGError.Handled = True

End Sub

'****************************************************************************
' PROCEDURE:    ReportRunningScript
' PURPOSE:        Report that an APG script is being executed.
'****************************************************************************

Sub ReportRunningScript(oScript)

    ' Use our ASP ErrorHandler (defined above)
    oScript.OnError = GetRef("APGErrorHandler")

    ' Report the script just before we run it
    Response.Write "Running " & oScript.Filename & " ..."    
    Response.Flush        
    
End Sub

'****************************************************************************
' PROCEDURE:    ReportBuiltPage
' PURPOSE:        Report that a page was built.
'****************************************************************************

Sub ReportBuiltPage(oScript)

    ' Only report files that are successfully built.
    If (oScript.GetLastError.Number <> 0) Then Exit Sub

    ' Use the oAPGen Util object to compute the relative path of the
    ' generated file from the root to the output file.
    Dim sRelPath
    sRelPath = oScript.Util.BuildRelativePath(oScript.APGen.OutputDir, oScript.Output.Path)
    
    ' Report the page that was generated
    Response.Write "<SPAN style='position:absolute;left:50%'><B>" & sRelPath & "</B> generated.</SPAN><BR />" & vbCRLF    
    
End Sub


' List the APG scripts to be run
Dim rgAPGScripts
rgAPGScripts = Array(_
        "default.apg", _
        "main.apg", _
        "error.apg" _
                    )

Sub BuildPages()
    '
    ' Run all of the APG scripts in rgAPGScripts
    '

    ' create the oAPGen object
    Dim oAPGen
    Set oAPGen = Server.CreateObject("APGen")

    ' set the default output dir to the root
    oAPGen.OutputDir = Server.MapPath(".")

    ' Loop through the files
    Dim sScriptFile
    For Each sScriptFile In rgAPGScripts

        ' Open the  script
        Dim oScript
        Set oScript = oAPGen.OpenScript(Server.MapPath("./apg/" & sScriptFile))

        ' Report which script is being run.
        ReportRunningScript oScript
        ' Run the script
        oScript.Run
        ' Report page if successfully built.
        ReportBuiltPage oScript
    Next
End Sub


' Page should expire immediately and not be cached in the browser
Response.Expires = -1
Response.CacheControl = "no-cache"

%>
<HTML>
<HEAD>
    <TITLE>Build Page</TITLE>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
</HEAD>
<BODY>
<H2>Administrators Only: The APGen Build Page</H2>
<%

' Build all the pages in the site
BuildPages

If nErrorCount = 0 Then
    %><H3>The site was successfully built</H3>
    <P><A href=".">Click here</A> to view the site.</P>
<%
Else
    %><H3>One or more errors have occurred.    Please fix the error(s)
        and run build.asp again.</H3>
<%
End If

%>
</BODY></HTML>