If you click on the //Failed to...// error message, it will expand to show the full stack trace. You'll probably find that the real cause of the error is:
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: 4
Which means that the input string is shorter than 4 characters.
What is $text
supposed to be? Why are you keeping only the first four characters? Would #if ($text.startsWith('Tags'))
be an equivalent check?
In XWiki you can also use $stringtool, which is actually StringUtils from Apache Commons Lang 3, and which has error-preventing methods for extracting a piece of text, or extracting a prefix, or checking for a prefix, among others.