I've run into an issue with safe operators and exc...
# lucee
d
I've run into an issue with safe operators and exceptions that seems to affect Lucee versions 5 - 7: https://dev.lucee.org/t/safe-operators-and-exception-handling/15217 In a nutshell, the use of the safe operator is swallowing exceptions which is different from ACF. Is this a bug or intended behavior?
b
In my experience, Lucee has always been more lax about the sorts of errors they "swallow" in safe operations
I'm not exactly sure how they implement it, but I've always assumed it used try/catch due to the behavior you pointed out.
Lucee makes calling that method "extra safe" 🙂
d
I can certainly see an argument for having it swallow exceptions, I mean it is called a "safe" operator, but I certainly wasn't expecting the behavior. If this is the intended behavior, it should definitely be documented since it differs from ACF.
b
yeah, "safe" has always been a little vague in that it even allows non existent methods to be called on an object https://trycf.com/gist/f07d4afb35b580a9a3f56ab3081e9ed8/lucee5?theme=monokai
d
That is at least documented.
b
So long as you chain the safe nav to it, a giant safety net is tossed over the left hand side of the operator
I personally don't think it should ignore actual cfthrow calls, but I'm not 100% if that was on purpose or on accident as the result of simply try/catching the entire LSH of the operator
Boxlang will do the method one, but not the headless function call as we only apply the safety to the actual dereferencing of the RHS expression
We (BoxLang) also won't catch the thrown exception, like Adobe. We only ignore not-existent keys or methods specifically
d
As a language that's supposed to help w/RAD, I like having the option to basically say "it's safe to ignore exceptions and return this instead", but I certainly didn't expect that behavior and I don't know that I'd argue that's how the safe operator should work (especially since it's different from ACF). Seems like there should be something close to a combination of the null coalescing and elvis operators, which intentionally swallows exceptions on the left hand, maybe something like:
Copy code
throw("hey") ??: "ignore exception"
That way you have control of the behavior.
b
That's an interesting suggestion. It raises the question of how you could control the types of exceptions to cache, and whether the actual exception would be available to the RHS. You're basically reinventing the try/catch wheel but as an expression instead of a statement. I'd honestly be more interested in creating a functional approach rather than an operator based one
Copy code
result = try(
  ()=>throw("hey"),
  (e)=>println( e.message )
)
d
I would think you'd swallow all exceptions, that would be the purpose: "If this code returns null, does not exist or throws an exception, then use the right hand expression"
b
My suspicion has always been that Lucee's issues arise from considering elvis and safe nav has having to do with ignoring errors in the first place. I can't speak for Adobe, but BoxLang isn't try/catch anything for sure. It simply passes a "safe" flag to the act of dereferencing the key or method, and if it's not found, then it kicks in. Exceptions never are thrown or play into it.
d
If you need robust exception handling based on the type, then I'd say stick to try/catch
b
My brain was still pondering this in the background when I thought of a functional approach to handing a task which may complete exceptionally using Completable Futures. This is Boxlang Code as it uses our
FutureNew()
BIF, but you could do it directly with Java completable futures, whcich are akin to JS promises.
Copy code
result = FutureNew( ()->randRange(1,2)==1 ? throw("hey") : "success" )
  .onError( e->e.message )
  .get()
or, you can even add logic that runs if it succeeds, which doesn't affect the "error path"
Copy code
result = FutureNew( ()->randRange(1,2)==1 ? throw("hey") : "success" )
  .then( v-> "result: " & v )
  .onError( e->e.message )
  .get()
I really like the functional approach to being able to wrap up a bit of logic and then provide a nice fluent API around how to handle its result
d
The functional approach is very nice for sure!
b
Anyway, language design is always an interesting tug-of-war between adding more top level constructs or operators to the language vs introducing a class/API to accomplish the same thing.
CF's
runAsync()
is similar, but we thought they nerfed their power too much (compared to Java's native completablefuture classes)
d
@zackster confirmed this was a bug, so I filed an issue: https://luceeserver.atlassian.net/browse/LDEV-5671
👍 1