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({throwable})>

    <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({throwable})
  }
  catch (any e) {
    writeOutput("Error: " & e.message);
  }
</cfscript>

CFTRY / CFCATCH example in Lucee

<cfscript>
  try {
    x=1/0;
  }
  catch (e) {
    throwableException = createObject( "java", "java.lang.Throwable" ).init( e.StackTrace );

    try {
      frapiClass = createObject("java", "com.intergral.fusionreactor.api.FRAPI")
      frapi = frapiClass.getInstance()
      frtxn = frapi.getActiveMasterTransaction()
      frtxn.setTrappedThrowable(throwableException)
    }

    catch (any e2) {
      writeOutput("Error: " & e2.message);
    }

    Dump(e);
  }
</cfscript>

CF onError example in Lucee

public void function onError(required any error) {
  var throwableException = createObject( "java", "java.lang.Throwable" ).init( error.StackTrace );

  try {
    frapiClass = createObject("java", "com.intergral.fusionreactor.api.FRAPI")
    frapi = frapiClass.getInstance()
    frtxn = frapi.getActiveMasterTransaction()
    frtxn.setTrappedThrowable(throwableException)
  }

  catch (any e2) {
    writeOutput("Error: " & e2.message);
  }

  Dump(error);
}

CFTRY / CFCATCH example in ColdFusion

<cfscript>
  try {
    x=1/0;
  }
  catch(any e) {
    throwableException = createObject( "java", "java.lang.Throwable" ).init( e );

    try {
      frapiClass = createObject("java", "com.intergral.fusionreactor.api.FRAPI")
      frapi = frapiClass.getInstance()
      frtxn = frapi.getActiveMasterTransaction()
      frtxn.setTrappedThrowable(throwableException)
    }

    catch (any e2) {
      writeOutput("Error: " & e2.message);
    }

    writeDump(var = e, label = "e");
  }
</cfscript>

CF onError example in ColdFusion

public void function onError(
  required any error,
  required any eventName
  )
  {
  var throwableException = createObject( "java", "java.lang.Throwable" ).init( error );

  try {
    frapiClass = createObject("java", "com.intergral.fusionreactor.api.FRAPI")
    frapi = frapiClass.getInstance()
    frtxn = frapi.getActiveMasterTransaction()
    frtxn.setTrappedThrowable(throwableException)
  }
  catch (any e2) {
    writeOutput("Error: " & e2.message);

  }
  writeDump(var = error, label = "error");

}