我遇到一个问题,其中onError方法在我的application.cfc文件中不再起作用。该程序昨天捕获了错误并正确显示error.cfm,但现在我只收到500错误。我的经理昨天可能已更改了文件中的几项内容,但我们似乎已重新创建了该文件,但未解决任何问题。

我目前使用onError,但尝试了cferror。使用cferror,数据输入表单甚至都不会加载。

代码如下。 onError被阻止,因为我正在使用cferror代码显示它。只需删除cferror并阻塞即可,如果要使用它将onError:

<!--- this component controls the application's global settings / event
handlers and maintains user sessions --->
<cfcomponent>

  <!--- define some basic settings --->

  <cfset this.name = "QualityDataPortal" />
  <cfset this.sessionManagement = "yes" />
  <cfset this.setClientCookies = "no" />
  <cfset this.loginStorage = "session" />
  <!--- this function is triggered when our application is initialized --->
  <cffunction name="onApplicationStart" access="public" returntype="boolean" output="no">
    <!--- define application variables --->
    <cfset application.dataSource = 'quality' />
    <!--- return out --->
    <cfreturn true />
  </cffunction>
  <cferror
    template="error.cfm"
    type="exception"
    mailTo="[email protected]" />
  <!--- <!--- this function is triggered when coldfusion encounters an error --->
    <cffunction name="onError" access="public" returntype="void" output="no">
      <cfargument name="exception" required="yes">
      <cfargument name="eventname" type="string" required="yes">

      <!--- send a dump of the error via email --->
      <cfmail from="[email protected]" to="[email protected]" subject="Quality Data Portal Error Encountered" type="html">
        <cfoutput>
          The following error was encountered on #dateformat(now(), 'dddd mmmm dd, yyyy')# at #timeformat(now(), 'hh:mm:ss tt')#<br /><br />
          <cfdump var="#arguments.exception#">
          <cftry><cfdump var="#arguments#"><cfcatch></cfcatch></cftry>
          <cfdump var="#form#">
          <cfdump var="#session#">
          <cfdump var="#cgi#">
        </cfoutput>
      </cfmail>

      <!--- alert the user that an error has been encountered --->
      <cflocation url="error.cfm" addtoken="no">
      <cfabort />

    </cffunction> --->

  <!--- this function is triggered when coldfusion receives a request for a template it cannot locate --->
  <cffunction name="onMissingTemplate" access="public" returntype="void" output="no">
    <cfargument name="targetPage" type="string" required="yes">
    <!--- alert the user that the page they requested could not be found --->
    <cflocation url="404.cfm" addtoken="no">
    <cfabort />
    <!--- return out --->
    <cfreturn />
  </cffunction>
</cfcomponent>

最佳答案

我建议,这样可以更好地看到,删除未使用的代码(cferror标记),删除不必要的注释,不要嵌套注释。

然后,从onError的此代码开始

<cffunction name="onError" returntype="void" output="false">

    <cfargument name="exception" required="true">
    <cfargument name="eventname" type="string" required="true">

    <cfmail to="[email protected]" from="[email protected]" subject="Quality Data Portal Error Encountered" type="html">

        <cfoutput>The following error was encountered on #DateFormat(now(), "dddd, mmmm dd, yyyy")# at #TimeFormat(now(), "HH:nn:ss")#</cfoutput>
        <hr />
        <cfdump var="#exception#" label="Exception" />
        <cfdump var="#form#" label="Form" />
        <cfdump var="#session#" label="Session" />
        <cfdump var="#cgi#" label="CGI" />

    </cfmail>

    <cflocation url="error.cfm" addtoken="false" />

</cffunction>


注意:


请注意:#timeformat(now(), 'hh:mm:ss tt'),应在几分钟内使用nn而不是mm
<cflocation ...之后没有代码,因为ColdFusion不会运行它。



  停止执行当前页面并打开ColdFusion页面或HTML文件。
  (https://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7cac.html

07-27 19:31