Well, that's a pretty useless error message... I can't tell whether it's complaining that you need to actually write "function(unit, cache, textframe)" and "end" around the text, or whether it's complaining that it's not getting a string (which could only happen if it's being stupid and calling the function for a nonexistent unit, such as the target unit when you aren't targeting anything). Try adding either the blue parts or the green parts, or maybe both:
Code:
function(unit, cache, textframe)
local name, _, text = UnitCastingInfo(unit)
if not name then
name, _, text = UnitChannelInfo(unit)
end
if not name then
name = UnitName(unit)
end
return text or name or ""
end