【问题标题】:Coldfusion error handling冷融合错误处理
【发布时间】:2013-02-14 22:09:32
【问题描述】:

我正在尝试创建一些自定义错误页面,通过电子邮件向我们的开发人员发送冷融合中出现 404 或 500 错误的信息。如果在电子邮件中提供错误变量也会很有帮助。

我在 Coldfusion 中设置了一个错误页面,并将 IIS(7) 指向它。

另外,在我的 application.cfm 文件中,我声明了以下内容:

<cferror type="exception" template="#name#">
<cferror type="request" template="#name#"> 
<cferror type="validation" template="#name#"> 
<cferror type="monitor" template="#name#"> 

我的问题是在错误文件中我不能包含任何 CF 调用,CF 似乎将它们剥离。

我做错了吗?

提前致谢!

【问题讨论】:

    标签: coldfusion coldfusion-9


    【解决方案1】:

    根据下面 Travis 的 cmets - 虽然仍支持使用 cferror 标签,但建议将 Application.cfm 转换为 Application.cfc(如果尚未)并使用 onError 方法反而。我在下面列出的所有限制在使用 onError 方法时都不适用,并且所有 CFML 功能都可用。 Here is the ColdFusion 9 documentation for the onError method.

    ColdFusion documentation regarding error handling 有一整节的详细信息。但是对于您关于cferror 标签的具体问题,您需要意识到发生错误时ColdFusion 可以执行的操作是有限制的,甚至取决于错误的类型。以下是摘自this page

    下表列出了 适用于错误申请页面的规则和注意事项:

    验证

    • 不能使用 CFML 标签
    • 可以使用 HTML 标签
    • 可以使用 Error.InvalidFieldsError.validationHeaderError.validationFooter 变量,方法是用数字符号 (#) 将它们括起来
    • 不能使用任何其他 CFML 变量

    请求

    • 不能使用 CFML 标签
    • 可以使用 HTML 标签
    • 可以使用九个 CFML 错误变量,例如 Error.Diagnostics,用数字符号括起来
    • 不能使用其他 CFML 变量

    例外

    • 可以使用完整的 CFML 语法,包括标签、函数和变量
    • 可以使用九个标准 CFML 错误变量和 cfcatch 变量。使用Errorcferror 作为这两种变量的前缀
    • 可以使用其他应用程序定义的 CFML 变量
    • 要显示任何 CFML 变量,请使用 cfoutput 标签


    摘自this page 关于每种异常类型的可用错误变量:

    仅验证

        error.validationHeader  Validation message header text.
        error.invalidFields     Unordered list of validation errors.
        error.validationFooter  Validation message footer text.
    

    请求和例外

        error.diagnostics       Detailed error diagnostics from ColdFusion.
        error.mailTo            E-mail address (same as value in cferror.MailTo).
        error.dateTime          Date and time when error occurred.
        error.browser           Browser that was running when error occurred.
        error.remoteAddress     IP address of remote client.
        error.HTTPReferer       Page from which client accessed link to page where error occurred.
        error.template          Page executing when error occurred.
        error.generatedContent  The content generated by the page up to the point where the error occurred.
        error.queryString       URL query string of client's request.
    

    仅例外

        error.message           Error message associated with the exception.
        error.rootCause         The root cause of the exception. This structure contains the information that is returned by a cfcatch tag.
                                For example, for a database exception, the SQL statement that caused the error is in the error.RootCause.Sql variable.
                                For Java exceptions, this variable contains the Java servlet exception reported by the JVM as the cause of the "root cause" of the exception.
        error.tagContext        Array of structures containing information for each tag in the tag stack. The tag stack consists of each tag that is currently open.
        error.type              Exception type.
    

    注意:如果 type = "exception",你可以用前缀 cferror 代替 Error;例如,cferror.diagnostics、cferror.mailTo 或 cferror.dateTime。

    【讨论】:

    • 我不得不投反对票,因为您提供的所有详细信息都使用了 cferror,这仍然可以接受,但在 application.cfc 中使用 onerror 函数会更好。你不受任何这些东西的限制,也不必担心类型。在 onError 中,您可以使用任何标签或脚本进行任何处理,并且可以像正常请求一样完全访问每个变量范围。
    • 没问题 - 我同意你的看法。我只是在 OP 专门询问cferror 时才发布此消息。我只能假设这意味着他仍在使用Application.cfm。我认为只回答这个问题然后为 OP 带来一个全新的架构会更容易。我相信这解释了为什么“CF 似乎将它们剥离”给 OP。不过,你的观点是有效的。
    • 有效假设。您实际上可以在 application.cfc 中使用 cferror,并且只需花费很少的精力就可以转换为 application.cfc,在 cf9 上,它早就该了
    • 谢谢@Travis。我将在此答案的开头添加一条注释,即最好/最好使用Application.cfconError 函数。
    • 我为 onError 方法的文档添加了参考和链接来解释这一点。再次感谢@Travis。
    【解决方案2】:

    您的错误文件是否具有 .cfm 扩展名?

    此外,如果您每发送一封带有 404 的电子邮件,您可能会面临邮件炸弹攻击。您可能会更好地添加诸如 Google Analytics 之类的内容来检查 404。 500 错误可能意味着很多事情都出错了,所以 CF 可能无论如何都不会发送电子邮件。

    【讨论】:

    • 我不会让自定义页面通过电子邮件发送 400 个错误,只有 500 个错误。该文件是一个 .cfm 文件。错误页面确实显示它只是删除了任何令人讨厌的 cf 调用,因为我希望能够在文件中使用 cf 邮件等。
    【解决方案3】:

    出于这个原因,使用模板最准确,例如... dbc.cfm。 在 dbc.cfm 中编写邮件发送代码。

    <cftry>
            <cfquery name="GetData" datasource="#Application.ds#" dbtype="ODBC" username="#Application.UserName#" password="#Application.Password#">
                SELECT m.price
                FROM productvendorsmap m
                WHERE m.productid = <cfqueryparam cfsqltype="cf_sql_integer" value="#ProductId#">
                AND m.vendorid = <cfqueryparam cfsqltype="cf_sql_integer" value="#VendorIdd#">
            </CFQUERY>
            <cfcatch type="Database">
                **<cfinclude template="dbc.cfm">**
            </cfcatch>
        </cftry>
    

    【讨论】:

      猜你喜欢
      • 2011-03-22
      • 1970-01-01
      • 1970-01-01
      • 2011-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多