This morning, I used to be operating into a wierd null-reference error in my ColdFusion dependency injector (DI). A CFProperty
-based element wasn’t being injected; however, no error was being thrown or logged. After commenting-out a bunch of code, I lastly narrowed it all the way down to a bug in ColdFusion. Should you use the safe-navigation operator to invoke a element technique, any error thrown in that technique might be swallowed up and the tactic might be short-circuited. I confirmed this habits in each Adobe ColdFusion (ACF) 2021 and 2023.
To see this in motion, let’s create a ColdFusion element that invokes three strategies. The second technique within the process will not exist and ought to throw an error:
element {
public void perform setData() {
this.information = {};
this.information.append({ key1: "value1" });
this.information.appenddddddddd({ key2: "value2" }); // <--- ERROR.
this.information.append({ key3: "value3" });
}
}
As you possibly can see, the second technique name to .appenddddddddd()
is nonsense and may throw an error. In our first take a look at, we’ll invoke the .setData()
technique utilizing the safe-navigation operator:
<cfscript>
factor = new Factor();
factor?.setData(); // <--- this line SHOULD error.
writeDump( factor );
</cfscript>
If we run this code in both ACF 2021 or 2023, we get this web page output:
As you possibly can see, no error was noticed within the top-level script—the code appeared to run accurately. Nonetheless, if we look at the general public keys on the Factor.cfc
occasion, we will see that solely key1
was set. The decision to set key2
errored-out and the .setData()
technique was short-circuited. However, no error was raised within the calling context (or logged to the console).
If we attempt to run the identical code with out the safe-navigation operator, we get the anticipated expertise:
<cfscript>
factor = new Factor();
factor.setData();
writeDump( factor );
</cfscript>
As you possibly can see, that is the identical code besides that we’re executing .setData()
as an alternative of ?.setData()
. And, once we run this ColdFusion code, we get the anticipated error:
That is the anticipated habits. That is what the safe-navigation operator ought to be doing as effectively. To get round this, I can after all rewrite the safe-navigation operator to make use of a structKeyExists()
situation:
<cfscript>
factor = new Factor();
if ( structKeyExists( factor, "setData" ) ) {
factor.setData(); // <--- this line SHOULD error.
}
writeDump( factor );
</cfscript>
However, that is only a work-around. I’ll file a bug within the Adobe Bug Tracker and hyperlink to it within the feedback.
Wish to use code from this submit?
Take a look at the license.
https://bennadel.com/4721