Skip to main content
Inspiring
March 22, 2013
Question

How to handle errors if error component doesn't exist yet

  • March 22, 2013
  • 2 replies
  • 3322 views

Traditionally, when I encapsulate code in a <CFTRY> block, I have it use the 'APPLICATION.coms.fw.error' component to handle processing of that error.

This is all well and fine, but during the first call to the application, where everything is starting up, lots of processing is done reading configuration files to get pathing, etc.  And that code has <CFTRY> blocks, but the thing is, they wouldn't be able to all the error component (because it hasn't yet been instantiated).

So should I just do something like:

<cfif isDefined( 'APPLICATION.coms.fw.error' )>

<!--- Handle Error Via Component. --->

<cfelse>

<!--- Output message to screen, use CFLOG to log error and the CFABORT to halt further processing. --->

</cfif>

    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    Community Expert
    March 23, 2013
    glamorous_Wonder6C1C
    Inspiring
    March 23, 2013

    Hi Aegis Kleais,

    "And that code has <CFTRY> blocks, but the thing is, they wouldn't be able to all the error component (because it hasn't yet been instantiated)"

    -- What if if you use only try catch to handle it. I am not sure about the purpose of using "APPLICATION.coms.fw.error" but it seems that you can try simple try catch to handle it.

    <cftry>

    //your code here

    <cfcatch type="any">

    </cfcatch>

    </cftry>

    Inspiring
    March 23, 2013

    @saurav

    The APPLICATION.coms.fw.error is a component that has been instantiated into the APPLICATION scope (so that it is available to all user across the application) and is designed to handle errors that we've "handled" via CFTRY/CFCATCH blocks.  You don't need the TYPE attribute if you want to use "any" as it is the default action.

    @BKBK

    I read the entire article and it goes into basic tools that ColdFusion has for handling errors (CFTRY/CFCATCH) using the onError() method in the application.cfc, etc.  But it doesn't seem to address the issue I'm having.  I want to be able to call my error handler component stored in APPLICATION.coms.fw.error, but I don't know if it exists yet.

    What I've taken to trying is to using the CFMODULE tag inside my CFCATCH tags.  It points to a 'handled-exceptions.cfm' module that has code in it which does the following:

    <cfif THISTAG.executionMode eq 'start'>

         <cfif isNull( APPLICATION.coms.fw.error )>

              <!--- The error-handling component isn't yet instantiated. --->

              <cflog file="#ATTRIBUTES.logFilename#" text="#ATTRIBUTES.logMessage#" type="#ATTRIBUTES.logSeverity#" />

              <cfdump var="#ATTRIBUTES.logMessage#" abort="true" />

         <cfelse>

              <!--- The error-handling component exists, so let's use it instead. --->

              <cfset APPLICATION.coms.fw.error.processHandledException(

                   message = "#ATTRIBUTES.logMessage#"

              ,     catchData = ATTRIBUTES.catchData

              ) />

         </cfif>

    <cfelse>

    </cfif>

    That way, whenever I need to determine if the error component exists AND call it at the same time, I can do something like:

    <cftry>

         <!--- Do something here that causes an error. --->

         <cfcatch>

              <cfmodule

                   template="/path/to/handled-exceptions.cfm"

                   attributecollection="#{

                        message = 'This is a message about the error we caught'

                   ,     catchData = CFCATCH

                   }#" />

         </cfcatch>

    </cftry>

    This has been about the best implementation I've found.

    BKBK
    Community Expert
    Community Expert
    March 23, 2013

    Aegis Kleais wrote:

    I read the entire article and it goes into basic tools that ColdFusion has for handling errors (CFTRY/CFCATCH) using the onError() method in the application.cfc, etc.  But it doesn't seem to address the issue I'm having.

    If your requirement falls outside those standard error-handling methods, then it is very likely that the requirement involves some over-designing.

    The APPLICATION.coms.fw.error is a component that has been instantiated into the APPLICATION scope (so that it is available to all user across the application)

    It is unclear to me whether APPLICATION.coms.fw.error is an application variable (in which you've stored an instance of a CFC created, for example, using createObject) or the path to error.cfc in dotted notation(that is, the path /application/coms/fw/error.cfc).