Try/catch block in TL

Hello!

I am trying to use the reformat component to convert data types in Clover 2.9.6. The problem is that I want to skip records that fail conversion instead of having the graph execution failing. I read in the TL documentation that I could use a try/catch block but I am having a lot of trouble making it work. Currently, I am running the following code:


//#TL
string message;
function transform() {
string errMsg;
try {
$0.valores :=  iif(isnull($0.valores), null, try_convert($0.valores, decimal));
} catch (errMsg) {
message = 'Failed for field $0.valores value '+$0.valores+' msg '+errMsg;
return -1;
}
}

function getMessage() {
return message;
}

If I remove the try/catch block, leaving only the code in the “try” section it works, but I need the error handling. The error message that clover throws is the following:

Encountered “$0.valores” at line 6, column 1.
Was expecting one of:
“;” …
“{” …
“}” …
“break” …
“continue” …
“for” …
“foreach” …
“if” …
“return” …
“while” …
“switch” …
“do” …
“try” …
“read_dict” …
“write_dict” …
“delete_dict” …

“isnull(” …
“nvl(” …
“nvl2(” …
“iif(” …
“print_stack(” …
“breakpoint(” …
“raise_error(” …
“print_err(” …
“eval(” …
“eval_exp(” …
“print_log(” …
“sequence(” …
“lookup(” …
“lookup_next(” …
“lookup_found(” …
“lookup_admin(” …

Please notice that “valores” is a valid input field for the reformat node.

Hello,
the problem is, that CTL1 allows mapping as the last statement only, so you need to move the mapping to separate function:

//#TL
string message;
string errMsg;

function mapValores(){
	$0.valores :=  iif(isnull($0.valores), null, num2str($valores));
}

function transform() {
	try{
		mapValores();
		return 0;
	}catch (errMsg) {
		message = 'Failed for field $0.valores value '+$0.valores+' msg '+errMsg;
		return -1;
	}
}

function getMessage() {
return message;
}