Question

I am currently trying to capture all errors in my application by including the following code in 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>

Some of that is borrowed from other examples I have seen (which I don't fully understand). I ultimately want to show some kind of graceful error page to solicit feedback from the user and then log/email the error. This seems to catch a lot of errors, but not all. I don't want to use try/catch everywhere if I don't have to either. Any suggestions?

Was it helpful?

Solution

There is also an overall ColdFusion error handler that you can define in the ColdFusion administrator. Under the Server Settings > Settings, scroll down to the bottom and set the option for "Site-wide Error Handler".

Check this in the docs as well About error handling in ColdFusion

OTHER TIPS

Using shared hosting shouldnt be an issue, just ask your hosr what the error templates are, if they are clued up about cf then they will have them setup. Wr use error.cfm and 404.cfm fot example which go in the root of every customers site.

The "OnError" method within the Application.cfc will only catch the errors where they have not previously been caught by a user defined try/catch statement.

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

With this said, I think it to be a good idea to have try catch statements within your code where necessary (situations where you cannot degrade gracefully). What I like to do is instantiate a cfc that wraps all exception handling. This cfc can then contain the actual error handling logic and all that the OnError method will need to do is instantiate the correct component and "control" the error.

A very simple illustration:

<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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top