【问题标题】:How do you capture errors for an entire Application in ColdFusion?您如何在 ColdFusion 中捕获整个应用程序的错误?
【发布时间】:2013-03-11 05:51:07
【问题描述】:

我目前正试图通过在 Application.cfc 中包含以下代码来捕获我的应用程序中的所有错误:

<cffunction name="onError">
    <!--- The onError method gets two arguments: 
            An exception structure, which is identical to a cfcatch variable. 
            The name of the Application.cfc method, if any, in which the error 
            happened. --->
    <cfargument name="Except" required=true/>
    <cfargument type="String" name = "EventName" required=true/>
    <!--- Log all errors in an application-specific log file. --->
    <cflog file="#THIS.NAME#" type="error" text="Event Name: #Eventname#" >
    <cflog file="#THIS.NAME#" type="error" text="Message: #Except.message#">
    <!--- Throw validation errors to ColdFusion for handling. --->
    <cfif Find("coldfusion.filter.FormValidationException", Arguments.Except.StackTrace)>
        <cfthrow object="#Except#">
        <cfelse>
        <cfoutput>
        <h1>#Eventname#</h1>
        </cfoutput>
        <cfdump var="#Except#">
    </cfif>
</cffunction>

其中一些是从我见过的其他示例中借用的(我不完全理解)。我最终想展示某种优雅的错误页面来征求用户的反馈,然后记录/发送错误。这似乎捕获了很多错误,但不是全部。如果我不需要,我也不想在任何地方使用 try/catch。有什么建议吗?

【问题讨论】:

  • 它没有发现什么错误?它应该捕获该应用程序中的所有异常。
  • @AdamCameron,好吧,我应该对此进行限定,并说我在 onError 下的代码似乎没有处理一些错误。似乎我记得几个错误,其中错误的方法和转储没有像预期的那样输出。我不记得在什么情况下,所以我需要玩一下,看看我是否可以复制。
  • 这是一个关键信息,是的,是的。
  • @AdamCameron,看起来问题可能是因为我正在执行 以解决服务器端验证错误。我从文档中复制了那段代码,但并没有真正理解它是什么。
  • 不是您问题的答案,但可能有用 - 您可能想调查 BugLogHQ - 比简单的 cflog 提供更多可见性,包括设置错误优先级和阈值电子邮件通知等。

标签: coldfusion railo cfml application.cfc


【解决方案1】:

您还可以在 ColdFusion 管理员中定义一个整体的 ColdFusion 错误处理程序。在 Server Settings > Settings 下,向下滚动到底部并设置“Site-wide Error Handler”选项。

在文档中也可以查看 About error handling in ColdFusion

【讨论】:

  • 我在共享主机环境中使用 Adob​​e ColdFusion。我在自己的 VPS 上使用 Railo,这意味着我无权访问 ACF 管理员。不过,我仍然会检查 Railo 提供的内容,这可能会很好。
  • @JamesRLamar - 你应该在 Application.cfc 中使用 OnError 方法来捕获大多数错误。您在&lt;cfthrow object="#Except#"&gt; 中有这一行,这会将错误抛出到下一个最高级别(可能是 CF 管理员)。您也可以替换该行并在本地处理这些错误。
  • 我认为那条线可能是罪魁祸首。我也在研究所有的文档......再次:(
【解决方案2】:

使用共享主机应该不是问题,只需询问您的 hosr 错误模板是什么,如果他们了解 cf,那么他们将进行设置。 Wr 使用 error.cfm 和 404.cfm fot 示例,它们位于每个客户站点的根目录中。

【讨论】:

    【解决方案3】:

    Application.cfc 中的“OnError”方法将仅捕获以前未被用户定义的 try/catch 语句捕获的错误。

    http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=appFramework_13.html

    话虽如此,我认为在必要时在代码中包含 try catch 语句是一个好主意(无法优雅降级的情况)。我喜欢做的是实例化一个包含所有异常处理的cfc。然后,这个 cfc 可以包含实际的错误处理逻辑,而 OnError 方法需要做的就是实例化正确的组件并“控制”错误。

    一个非常简单的插图:

    <cfscript>
    
      /** Application.cfc **/
      public function onError(required exception, required string eventName)
      {
        var factory = new App.ExceptionFactory();
        var e = factory.getNewException(arguments.eventName, arguments.exception);
    
        if (e.logError()) {
          /** we cauld also have a logging cfc etc **/
          var loggingFile = new App.SomeLoggingCfc(arguments.eventName, arguments.exception);
          loggingFile.commitLog();
        }
        if (e.debugError()) {
          // show developer info here
        }
    
        /** Throw the exception **/
        e.throwException();
      } 
    
      /** App.ExceptionFactory **/
      public ExceptionFactory function getNewException(required string eventName, required exception)
      {
        return new "App.#exception.type#"(argumentCollection = arguments);
      } 
    
      /** App.Exception.CustomException **/
      public boolean function logError()
      {
        /** log the error **/
      }
      public boolean function debugError() {}
    
      public function throwException()
      {
        /** do what you want here **/
      }
    
    </cfscript>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-11
      • 2012-12-07
      • 2019-03-22
      • 2014-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多