Timer script throws a KeyError, but scripting console doesn't

I've written a script for performing an http request that I execute every 60s. I'm requesting data from a server that I then set to tag values in my PLC. I run 3 instance of this script (I know it's inefficient to be performing so many tag writes, but it's been requested that the separate subsystems I'm polling are treated independently).

2 of my 3 scripts are running fine, but the one problem script is an enigma. I know the data packet I'm recieving from the request is a list of dictionaries and I know the dictionary keys. When I try to copy the one of vlaues at key 'data', Ignition is throwing a KeyError exception. I've wrapped everything in a try block and I'm throwing the results into my logs, and I am completely stumped.

First my script:

payload = {'names': tagPathUtil.cryoParams,'props': tagPathUtil.cryoProps,'ppmuser':'0'}
subSys = 'cryo'
directory = "/list/numeric/valueAndTime"
headers = {'Accept': 'application/json'}
response = mcrUtil.mcrGetRequest(payload, subSys, directory, tagPathUtil.cryoErrTags, headers=headers)

if response.good:
	y = response.json 	#Set y to the json interpretation of the response object (y is a list)
	tags = []			#Create an empty list for the tag value
	
	#Iterate through the response json list 
	try:
		for i in range(len(y)):
			tagData = y[i]['data'][0]	#Set tagData to the value of the parameter from the http request
			tags.append(tagData)		#Append tagData to the tag values list
	except Exception as e:
		message = 'Good http request. Failed on tag assignment. Exception type: %s. ' % e
		if isinstance(y,list):
			message += 'List returned. ' 
			if isinstance(y[0],dict):
				message += 'List of dictionaries. Dictionary keys: %s' % (y[0].keys())
			else:
				message += 'List of %s.' % (type(y[0]))
		else:
			message = 'Good http request. Failed on tag assignment. Type returned: .' % (type(y))
		
		system.util.getLogger('Data Logger').info(message)
	else:
		system.tag.writeBlocking(tagPathUtil.cryoTagPaths, tags)

And now an entry from my logs:

If it's hard to read, it says: "Good http request. Failed on tag assignment. Exception type: 'data'*. List returned. List of dicitonaries. Dictionary keys: ['data','type','entryid','tsource','property','device','tstamp']"
*I thought 'data' would read KeyError since the exception should be the KeyError

Some things that I have noticed:

  1. If I don't have a successful http request, it's a client side error (typically 400, but I've seen 406).
  2. If I have a successful http request, I get this exception.
  3. In the other 2 instances of this script, I am seeing consistent successful tansfers.
  4. If I run this script in the scripting console, I get Error_TypeConversion feedback in the console.

I've attached my logs below.
wrapper.log (5.2 MB)

I reset my gateway and the issue took care of itself, but I'm worrited about this popping up in the future.

'%s' % KeyError returns just the message of the KeyError.

Also, your exception handling is only telling you the keys in y[0] - my guess is the 'shape' of the data is not consistent for all the rows in the return.

2 Likes