在ColdFusion 10中捕获错误

时间:2013-11-09 18:02:34

标签: coldfusion coldfusion-10

我正在关注Ray The Complete Guide to Adding Error Handling to Your ColdFusion Application,并对一些可用的值感兴趣,但我在脚本onError中使用Application.cfc函数。

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

问:如果我在scripted 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.");
        }
    }
}

1 个答案:

答案 0 :(得分: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>