Error Handling in tag event script

Hello,
writing this script inside a tag event script generate a dispatcher log error as expected.

def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	if currentValue.value != previousValue.value :
        a = 2/0
        ...

If i write this function :

def handler(tag, id_richiesta):

    logger_name = "test"
    log = system.util.getLogger(logger_name)
    log.trace("log 1")
    a = 2/0
    log.trace("log 2")

I call this function form the tag event script like so :

def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	if currentValue.value != previousValue.value :
	    id_richiesta = currentValue.value
	    RequestHandler.handler(tag, id_richiesta)

The result is that I find the trace message "log 1" in the logger, then the script stops working without giving any message.

If I place "a = 2/0" inside a try-catch block, the script behaves as expected.

I would like to understand what is happening, as my aim is to write an event handler function that will always return a message code to the PLC, so I would like to understand why, in this case, the script ends silently.

At the moment my idea is to use something like this, so i can notify plc and get detailed error in the ignition logger.

def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	
	if currentValue.value != previousValue.value :
		id_richiesta = currentValue.value
		try : 
		    RequestHandler.handler(tag, id_richiesta)
		except : 
			## write to plc, not handled error
            ...
			raise

Thank you for your time.

I may not be understanding this correctly, but using the try-except block should solve your problem. Without it your script will encounter the unhandled exception following the 'Log 1' message and you will not be able to perform any additional actions.

If the handler function is executed without error, you can provide a success message after the function call in the try block (if necessary). Otherwise, I would write some logic in the except block to pass the message back to your PLC.

I am not sure if you are expecting any particular errors, but you can add several excepts to handle each of these and set the code you wish to return to the PLC accordingly. The user manual has some good information on error handling here

Hello,
Thank you for your answer.

To simplify my question, I would like to understand why an error gets notified by the ignition logger if it occurs inside a script written directly in the tag event script, but if the error occurs inside a project library function called by the tag event script, it won't be notified in the Ignition logger.

Test carefully. If you don't catch and log your own exceptions, you are at the mercy of the built-in logging, which generally changes the log level to DEBUG after the first error in a given script. DEBUG is hidden by default.

You must use try: - except: blocks if you want to do anything with an error.

4 Likes

I understand now, thank you.

As soon i set the script.dispatcher log level to debug, I'm able to see all the script error messages.

Is a bit confusing because the error messages are still marked with Error flag, but they only display if I turn on at least Debug mode but at least i know about this behavior.

Thank you for your time.