I need to introduce extra error handling in my code. This is because when my scripts run sometimes the outcome is successful when in fact it hasn’t done what it was supposed to do.
Is there any way that when the script reports an error that it exits and logs the result has failed?
put 4 into expected_value
put (2 + 2) into actual_value
if actual_value is not equal to expected_value
logError "This failed: Expected:"&&expected_value&&"Got:"&&actual_value
else
log "This passed: Expected:"&&expected_value&&"Got:"&&actual_value
end if
Any time LogError is used, the script will report as failing. You can also use LogWarning to alert you to possible problems, but the script will still pass. Chapter 9 in the EP reference goes into much more detail.
That’s exactly right. LogError can be really useful if you want to record an error, cause that test run to be listed as a failure, and continue running the rest of the test.
If you want the script to fail and stop running, you can either use exit all after the LogError command to stop the run, or you could throw an exception instead:
When used in a low-level function that may be called from a lot of places, this has the advantage of making it possible to catch the exception in some places where you call the function so the script can continue. If it’s not caught, the exception will cause the script to fail and stop immediately.