Exception handling in Funnelback Freemarker FTL files

We have a bit of code on our system which is failing at times, and when it does results in some ugly error code on the final webpage.

The Freemarker code in question is trying to return back to Squiz Matrix some markup code which is expecting a particular value ${s.result…} value to be populated, but without any default value (as per https://docs.funnelback.com/more/extra/freemarker.html#data-model-variables) or any attempt at exception handling where the ${s.results…} value is blank.

Are there any examples of exception handling in FTL files that anyone would recommend? I’ve found http://freemarker.org/docs/pgui_config_errorhandling.html#autoid_43 but an actual example would be useful

Hi Douglas,

To prevent the ugly exception from showing, it’s possible to use the following config option in collection.cfg:

ui.modern.freemarker.error_format=html

This will display any errors or exception as an html comment and preventing it being rendered for the end user.

Please see the follwong link for more information: UI Modern Freemarker Error Format (collection.cfg) - Funnelback Documentation - Version 15.10.0

Going forward though, it would be best to prevent the exception from happening in the first place by always being defensive when accessing any variable. You can do this by checking it if exists before using it. This is what I commonly do wit ``hin my templates:

<#if (s.result.summary)!?has_content>
   <#-- you code -->
</#if> 

The above makes use of the ! and ?has_content functions to test if a parameter exists and is not empty.

More info can be found in the freemarker manuals.
http://freemarker.org/docs/dgui_template_exp.html#dgui_template_exp_missing_default
http://freemarker.org/docs/ref_builtins_expert.html#ref_builtin_has_content

Hope this helps.

~Gioan

1 Like

Thanks Gioan. That was just what I was after.