1

我正在关注 Ray 的向 ColdFusion 应用程序添加错误处理的完整指南,并且对一些可用的值很感兴趣,但我onError在脚本中使用该函数Application.cfc

根据learn.adobe.com的说法,cferror 有一个诊断错误变量以及其他一些有趣的位,例如error.remoteAddresserror.generatedContent

问:如果我在脚本化 Application.cfc中使用 onError ,那么如何获取诊断错误变量(以及其他变量)?

编辑:您到底是如何使用的onError

public void function onSessionStart() {
    session.TryCatch = 3;
}

public boolean function onRequestStart(String targetPage){
    request.TickCount = GetTickCount();
    request.LogDBSort = 0;
    return true;
}

public void function onError(Exception,EventName) {
    if (StructKeyExists(session,"TryCatch") && session.TryCatch) {
        session.TryCatch -= 1;
        if (StructKeyExists(Exception,"Message")) {
            WriteOutput("It looks like you got the following ColdFusion error:<blockquote>"
                & Exception.Message
                & "</blockquote>"
            );
            local.LogCFErrMessage = Exception.Message;
        } else {
            local.LogCFErrMessage = "No Exception.Message";
        }
        param request.TickCount = GetTickCount();
        param request.LogDBSort = -999;
        request.LogDBSort += 1;

        if (StructKeyExists(Exception,"Name")) {
            local.LogCFErrName = Exception.Name;
        } else {
            local.LogCFErrName = "No Exception.Name";
        }
        if (StructKeyExists(Exception,"Detail")) {
            local.LogCFErrDetail = Exception.Detail;
        } else {
            local.LogCFErrDetail = "No Exception.Detail";
        }
        if (StructKeyExists(Exception,"Number")) {
            local.LogCFErrNumber = Exception.Number;
        } else {
            local.LogCFErrNumber = "No Exception.Number";
        }
        if (StructKeyExists(Exception,"Type")) {
            local.LogCFErrType = Exception.Type;
        } else {
            local.LogCFErrType = "No Exception.Type";
        }
        if (StructKeyExists(Exception,"EventName")) {
            local.LogCFErrEventName = Exception.EventName;
        } else {
            local.LogCFErrEventName = "No Exception.EventName";
        }
        local.svc = new query();
        local.sql = "
        DECLARE @LogCFErrSort Int = #Val(request.LogDBSort)#;
        DECLARE @LogCFErrNumber Int = #Val(local.LogCFErrNumber)#;
        DECLARE @LogCFErrElapsed Int = #GetTickCount() - request.TickCount#;
        INSERT INTO LogCFErr
        (LogCFErrSort, LogCFErrNumber, LogCFErrElapsed,LogCFErrName,LogCFErrDetail,LogCFErrMessage,LogCFErrType,LogCFErrEventName) VALUES(
        @LogCFErrSort,@LogCFErrNumber,@LogCFErrElapsed,?,?,?,?,?)
        ";
        local.svc.setSQL(local.sql);
        local.svc.addParam(cfsqltype="CF_SQL_VARCHAR",value=Left(local.LogCFErrName,512));
        local.svc.addParam(cfsqltype="CF_SQL_VARCHAR",value=Left(local.LogCFErrDetail,512));
        local.svc.addParam(cfsqltype="CF_SQL_VARCHAR",value=Left(local.LogCFErrMessage,512));
        local.svc.addParam(cfsqltype="CF_SQL_VARCHAR",value=Left(local.LogCFErrType,512));
        local.svc.addParam(cfsqltype="CF_SQL_VARCHAR",value=Left(local.LogCFErrEventName,512));
        local.svc.execute();
        if (IsDefined("Application.Home.Email.UserName")) {
            local.svc = new mail();
            local.svc.setSubject(GetApplicationMetaData().Name & ': ' & ListLast(GetBaseTemplatePath(),'\'));
            local.msg = LogCFErrMessage;
            local.svc.setBody(local.msg);

            local.svc.setServer(Application.Home.Email.Server);
            local.svc.setType(Application.Home.Email.Type);
            local.svc.setUseSSL(Application.Home.Email.UseSSL);
            local.svc.setPort(Application.Home.Email.Port);
            local.svc.setFrom(Application.Home.Email.UserName);
            local.svc.setUserName(Application.Home.Email.UserName);
            local.svc.setPassword(Application.Home.Email.Password);
            local.svc.setTo(Application.Home.Email.UserName);
            local.svc.Send();
            WriteOutput("I've sent an email to the administrator to let them know.");
        }
    }
}
4

1 回答 1

1

我使用arguments.exception。这是一个例子。

<cffunction name="onError">
<cfargument name="Exception" required=true type="any">
 more code

<cfmail to="#recipients#" 
from="#recipients#" 
subject="Error on whatever" type="html">

<p>Error Occurred #now()#</p> 
<cfdump var="#Arguments.Exception#" label="exception">
<cfdump var="#request#" label="request">
<cfdump var="#cgi#" label="cgi">
</cfmail>
于 2013-11-09T21:21:38.153 回答