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

**URL:** <https://forum.inductiveautomation.com/t/timer-script-throws-a-keyerror-but-scripting-console-doesnt/69577>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [February 8, 2023, 1:56pm UTC](https://forum.inductiveautomation.com/t/timer-script-throws-a-keyerror-but-scripting-console-doesnt/69577 "2023-02-08T13:56:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![L\_F](https://avatars.discourse-cdn.com/v4/letter/l/5f8ce5/32.png) [@L\_F](https://forum.inductiveautomation.com/u/L_F)\
**Post date:** [February 8, 2023, 1:56pm UTC](https://forum.inductiveautomation.com/t/timer-script-throws-a-keyerror-but-scripting-console-doesnt/69577/1 "2023-02-08T13:56:19Z")

</div>

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:

```auto
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:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/a/ea48cfc66305a871c20e3dcb3d3b1976b333f33f.png)

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](https://forum.inductiveautomation.com/uploads/short-url/guGsZ812SFfSIUgqKw3Rp4krR9i.log) (5.2 MB)

---

<div class="post-metadata">

**Author:** ![L\_F](https://avatars.discourse-cdn.com/v4/letter/l/5f8ce5/32.png) [@L\_F](https://forum.inductiveautomation.com/u/L_F)\
**Post date:** [February 8, 2023, 2:29pm UTC](https://forum.inductiveautomation.com/t/timer-script-throws-a-keyerror-but-scripting-console-doesnt/69577/2 "2023-02-08T14:29:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [February 8, 2023, 5:45pm UTC](https://forum.inductiveautomation.com/t/timer-script-throws-a-keyerror-but-scripting-console-doesnt/69577/3 "2023-02-08T17:45:31Z")

</div>

> [@L\_F](#):
>
> I thought 'data' would read KeyError since the exception should be the KeyError

`'%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.
