Skip to content

Adding FusionReactor Error context to handled errors in ColdFusion

Within ColdFusion, you have multiple ways to handle errors as they occur in ColdFusion.

The most common 2 being to utilize CFTRY / CFCATCH or use the onError function within your Application.cfc file.

When you do this, the Exception will be caught and not rethrown within the Java engine. Which will prevent FusionReactor from being able to track the error.

What you can do is use FRAPI to set the Error stack trace on your FusionReactor request without throwing the actual error.

FRAPI code example

Below is an example of how to safely initialize and use FRAPI to set the trapped throwable object.

# CF TAG EXAMPLE
<cftry>

    <cfset frapiClass = createObject("java", "com.intergral.fusionreactor.api.FRAPI")>
    <cfset frapi = frapiClass.getInstance()>
    <cfset frtxn = frapi.getActiveMasterTransaction()>
    <cfset frexception = frtxn.setTrappedThrowable({EXCEPTION VARIABLE})>

    <cfcatch type="any">
        Error: <cfoutput>#cfcatch.message#</cfoutput>
    </cfcatch>

</cftry>

# CF SCRIPT EXAMPLE
<cfscript>
  try {
  frapiClass = createObject("java", "com.intergral.fusionreactor.api.FRAPI")
  frapi = frapiClass.getInstance()
  frtxn = frapi.getActiveMasterTransaction()
  frtxn.setTrappedThrowable({EXCEPTION VARIABLE})
  }
  catch (any e) {
    writeOutput("Error: " & e.message);
  }
</cfscript>

CFTRY / CFCATCH example

<cftry>
    <cfthrow type="intergral" message="Thrown exception.">

    <cfcatch>
        <cfdump var=#cfcatch#>

    <cfscript>
        exception = createObject( "java", "java.lang.Throwable" ).init( cfcatch );
        stacktrace = cfcatch.stackTrace;
        exception.setStackTrace(cfcatch.getStackTrace());

        try {
          frapiClass = createObject("java", "com.intergral.fusionreactor.api.FRAPI")
          frapi = frapiClass.getInstance()
          frtxn = frapi.getActiveMasterTransaction()
          frtxn.setTrappedThrowable(exception)
        }
        catch (any e) {
          writeOutput("Error: " & e.message);
        }
    </cfscript>

    </cfcatch>
</cftry>

CF onError example

<cffunction name="onError">
    <cfargument name="Exception" required=true/>
    <cfargument type="String" name="EventName" required=true/>

    <!--- Log all errors. --->
    <cflog file="#This.Name#" type="error"
    text="Event Name: #Arguments.Eventname#" >

    <cflog file="#This.Name#" type="error" text="Message: #Arguments.Exception.message#">
    <cflog file="#This.Name#" type="error" text="Root Cause Message: #Arguments.Exception.rootcause.message#">

   <cftry>

    <cfset frapiClass = createObject("java", "com.intergral.fusionreactor.api.FRAPI")>
    <cfset frapi = frapiClass.getInstance()>
    <cfset frtxn = frapi.getActiveMasterTransaction()>
    <cfset frexception = frtxn.setTrappedThrowable({EXCEPTION VARIABLE})>

    <cfcatch type="any">
        Error: <cfoutput>#Arguments.Exception.message#</cfoutput>
    </cfcatch>

</cftry>

    </cfif>
</cffunction>