Troubleshooting Tag Event scripts

I’ve run into some occasions where something I’ve done is causing an Event Script to error out (I know this from the little red x that appears next to the tag in the browser). However, no error information goes to the console. What’s the best way of getting more information in these situations?

More questions regarding Tag Events: I seem to be having difficulty calling other routines from inside a routine called by a tag event. This same script works when it’s called from the script playground but fails when calling it from a Tag Event. For example:

import shared

if shared.util.serialExists(serialNumber) == True :
do something

I’ve also tried import shared.util with no difference.

Don’t import shared, it is already imported. Just call your shared function. For example:

if shared.util.serialExists(serialNumber) == True :
    do something

It seems that importing “shared” in Tag Event Scripts causes these scripts not to work. I get the error, “No module named shared”.

To catch Tag Event Script errors and report them in the console use a logger and try and except. Here is an example:

import sys
logger = system.util.getLogger("MyLogger")
try:
    if shared.util.serialExists(serialNumber) == True :
        do something
except:
    logger.error(str(sys.exc_info()[1]))    

Cheers,

I also get an error on any db query’s:

       import sys
	try:
		sel = "SELECT COUNT(ID) FROM InProcMain WHERE SerialNumber = '" + serialNumber + "'"
		if system.db.runScalarQuery(sel) > 0 :
			return True
		else :
			return False
	except :
		logger = system.util.getLogger("ALogger")
		logger.error(str(sys.exc_info()[1]))		
		return False

This get me: java.lang.Exception: Error executing system.db.runScalarQuery(SELECT COUNT(ID) FROM InProcMain WHERE SerialNumber = '12345', , )

The same thing works from the script playground or elsewhere. It doesn’t seem to matter if I use import system or not.

Kurt

You should show the entire error message. It will tell us why the SQL is incorrect. You should also try pasting all of your code. For example, where is serialNumber defined in the above example code?

That is the entire message. The query itself is valid. This only occurs when calling from an tag event.

Ah, sorry, I forgot this is Ignition 7.7, so I might not be able to help, but you could still try specifying the database name for the query. It might be that Ignition doesn’t know what default database to use (since Tag Events would have a “global” or “shared” scope):

      if system.db.runScalarQuery(sel, "DataBaseNameYouDefinedInIgnitionWebpage") > 0 :
         return True
      else :
         return False

Adam, is correct. Tag Event Scripts do not belong to any project so it doesn’t get any project’s default datasource. So you either need to specify the datasource or set a default datasource for the Realtime Tag Provider.

Here is proper exception reporting that will show the exception in the console:

	logger = system.util.getLogger("MyLogger")
	try:
		sel = "SELECT 10"
		value =  system.db.runScalarQuery(sel)
		logger.info("The value is %s"% value)
	except:
		import sys
		from java.lang import Exception
		exceptionType, exception, stack = sys.exc_info()
		if exceptionType == Exception:
			exception = exception.getCause()
		logger.error(exception.getMessage(), exception)

Is there a reason you are returning a value from a Tag Event Script? Returning a value from an Event Tag Script doesn’t do anything but return from the function.

1 Like

Specifying a db did the trick. In hindsight it seems obvious. Thanks

Hi,

Also, in 7.7.1 we’ll log some sort of message to the console when scripts error out. Currently they log on debug level against the tag provider logger (of the top of my head, I think it’s like “Provider[default]” for the “default” provider).

Regards,

When the tag providor log level is changed to debug or higher exceptions are not logged.

I also noticed that exceptions in Tag Event Scripts are not logged to the wrapper.log file.