Quantcast
Channel: MikroTik
Viewing all articles
Browse latest Browse all 15133

Scripting • Possible bugs with :onerror (new in 7.13)

$
0
0
The variable used to write the error message into to in the :onerror global command (new in 7.13) is not scoped to the in={} and do={} of the :onerror like the loop variable of a :foreach loop is. This is is not the behavior I expected and it caused me some issues before I figured it out.

Any idea if this is a bug or just undocumented behavior?

Script demonstrating the problem:
Code:
:put "#### Starting :onerror Tests ####\n":put "## Test 2: Error in scope with and before no error"{    :put "# Test 2 part 1: erroring :onerror"    :put "before :onerror \$ErrVar: $ErrVar"    :onerror ErrVar in={        :put "inside :onerror in={} \$ErrVar: $ErrVar"        :error "error msg 2p1"    } do={        put "inside :onerror do={} \$ErrVar: $ErrVar"    }    :put "Post :onerror in scope \$ErrVar: $ErrVar"    :put "\n# Test 2 part 2: not erroring :onerror"    :put "before :onerror \$ErrVar: $ErrVar"    :onerror ErrVar in={        :put "inside :onerror in={} \$ErrVar: $ErrVar"        #:put "msg 2p2"    } do={        put "inside :onerror do={} \$ErrVar: $ErrVar"    }    :put "Post :onerror in scope \$ErrVar: $ErrVar"}:put "\n### This demonstrates that \$ErrVar used in the :onerror is not scoped to the in={} and do={} of the :onerror.":put "### This is not the behavior that I would expect, and isn't documented.":put "\n#### Ended :onerror Tests ####"
Output of script:
Code:
#### Starting :onerror Tests ###### Test 2: Error in scope with and before no error# Test 2 part 1: erroring :onerrorbefore :onerror $ErrVar: inside :onerror in={} $ErrVar: inside :onerror do={} $ErrVar: error msg 2p1Post :onerror in scope $ErrVar: error msg 2p1# Test 2 part 2: not erroring :onerrorbefore :onerror $ErrVar: error msg 2p1inside :onerror in={} $ErrVar: error msg 2p1inside :onerror do={} $ErrVar: conditional not booleanPost :onerror in scope $ErrVar: conditional not boolean### This demonstrates that $ErrVar used in the :onerror is not scoped to the in={} and do={} of the :onerror.### This is not the behavior that I would expect, and isn't documented.#### Ended :onerror Tests ####Script file loaded and executed successfully
I have also found another instance where I get the same "conditional not boolean" error message in the error variable of the :onerror that I can't specifically identify a cause of. It seem to be resolved by declaring and then erasing the (already empty) error variable. This looks like it is almost certainly caused by a bug.

Script snippet of where this bug occurs (these are some sloppy test for a log/print output function I wrote):
Code:
########## Tests ############# For ErrOutFn ###:put "\n#### Tests for ErrOutFn ####";:global DEBUG true;:put "## Input validation that should pass":foreach Fn in={$ErrOutFn} do={    :put "";    :put "### LOOP ITERATION";    :put "";    :put "## Input Validation Tests";    :put "# Tests that should pass";    :put "\$ErrVal: $ErrVal"    {        :onerror ErrVal in={            $Fn "warning" "SomeFn" "should not error";        } do={            :put "Should not show error: $ErrVal";            :put "";        }    }    :put "\$ErrVal: $ErrVal"        :put "";    :put "# Tests that should fail";    {        :onerror ErrVal in={            $Fn ;        } do={            :put "Should show error (I provided no input): $ErrVal";            :put "";        }    }    :put "\$ErrVal: $ErrVal"    {        :onerror ErrVal in={            $Fn "info";        } do={            :put "Should show error (I provided only \$1): $ErrVal";            :put "";        }    }    :put "\$ErrVal: $ErrVal"    {        :onerror ErrVal in={            $Fn "info" "SomeFn";        } do={            :put "Should show error (I provided only \$1,\$2): $ErrVal";            :put "";        }    }    :put "\$ErrVal: $ErrVal"    {        :onerror ErrVal in={            $Fn "info" "SomeFn" "test 1" true;        } do={            :put "Should show error: $ErrVal";            :put "";        }    }    :put "\$ErrVal: $ErrVal"    {        :onerror ErrVal in={            $Fn "warning" "SomeFn" "test 2" 1;        } do={            :put "Should show error: $ErrVal";            :put "";        }    }    :put "\$ErrVal: $ErrVal"    {        :onerror ErrVal in={            $Fn "debug" "SomeFn" "test 3" "1";        } do={            :put "Should show error: $ErrVal";            :put "";        }    }    :put "\$ErrVal: $ErrVal"    {        :onerror ErrVal in={            $Fn "warning" "SomeFn" "test 4, default exit (do exit)"        } do={            :put "Should show error: $ErrVal";            :put "";        }    }    :put "\$ErrVal: $ErrVal"    $Fn "error" "SomeFn" "test 5, should not exit" "false";    {        :onerror ErrVal in={            $Fn "info" "SomeFn" "test 6, default exit (do exit)";        } do={            :put "Should show error: $ErrVal";            :put "";        }    }    :put "\$ErrVal: $ErrVal"    {        :onerror ErrVal in={            $Fn "debug" "SomeFn" "test 7, default exit (do exit)";        } do={            :put "Should show error: $ErrVal";            :put "";        }    }    :put "\$ErrVal: $ErrVal"}:put "\$Fn: $Fn"# I need to declare and reset $ErrVar otherwise I get a "condition is not boolean error:, probably a bug#:local ErrVal;#:set ErrVal;### For DbgFn ###:put "\n#### Tests for DbgFn ####";:foreach Fn in={$DbgFn} do={    :put "";    :put "### LOOP ITERATION";    :put "";    :put "## Input Validation Tests";    :put "\$ErrVal: $ErrVal"    {        :onerror ErrVal in={            $Fn ;        } do={            :put "Should show error (I provided no input): $ErrVal";            :put "";        }    }    :put "\$ErrVal: $ErrVal"    {        :onerror ErrVal in={            $Fn "SomeFn";        } do={            :put "Should show error (I provided only \$1): $ErrVal";            :put "";        }    }    # :local ErrVal;    # :set ErrVal;    :put "\$ErrVal: $ErrVal";    :put "\$ErrVal data type: $[:typeof "$ErrVal"]";}:set DEBUG;
Output of script:
Code:
#### Tests for ErrOutFn ###### Input validation that should pass### LOOP ITERATION## Input Validation Tests# Tests that should pass$ErrVal: **Dbg: warning,SomeFn: should not errorShould not show error: warning,SomeFn: should not error$ErrVal: # Tests that should failwarning: $OutBaseFn: previous log message had improperly specified severity $1 to this function should be: debug, info, warning, or error**Dbg: invalid,: Should show error (I provided no input): invalid,: $ErrVal: **Dbg: info,: Should show error (I provided only $1): info,: $ErrVal: **Dbg: info,SomeFn: Should show error (I provided only $1,$2): info,SomeFn: $ErrVal: **Dbg: info,SomeFn: test 1Should show error: info,SomeFn: test 1$ErrVal: **Dbg: warning,SomeFn: test 2Should show error: warning,SomeFn: test 2$ErrVal: **Dbg: debug,SomeFn: test 3Should show error: debug,SomeFn: test 3$ErrVal: **Dbg: warning,SomeFn: test 4, default exit (do exit)Should show error: warning,SomeFn: test 4, default exit (do exit)$ErrVal: **Dbg: error,SomeFn: test 5, should not exit**Dbg: info,SomeFn: test 6, default exit (do exit)Should show error: info,SomeFn: test 6, default exit (do exit)$ErrVal: **Dbg: debug,SomeFn: test 7, default exit (do exit)Should show error: debug,SomeFn: test 7, default exit (do exit)$ErrVal: $Fn: #### Tests for DbgFn ####### LOOP ITERATION## Input Validation Tests$ErrVal: **Dbg: debug,: Should show error (I provided no input): conditional not boolean$ErrVal: **Dbg: debug,SomeFn: Should show error (I provided only $1): conditional not boolean$ErrVal: $ErrVal data type: nothingScript file loaded and executed successfully

Statistics: Posted by ther33 — Sun Jan 07, 2024 11:38 pm



Viewing all articles
Browse latest Browse all 15133

Trending Articles