I am fairly new to python and need recommendations on how to handle internal/helper function failures.
I have a script that that imports a list of jurisdictions, then iterates through that list and creates a view, map, and experience builder application tailored to that jurisdiction. It's working more or less flawlessly but is routinely failing on the third iteration. And the failure happens due to an error with the arcgis\features\managers.py helper.
KeyError Traceback (most recent call last)
c:\Users\user\path_to_project\script.py in <cell line: 40>()
224 for j in jList: #changed 'jurisdiction' to 'j'
225 #*Create Views
---> 226 jView = masterLayer.manager.create_view(name = j + '_view', spatial_reference = sr, tags = tagsText, capabilities = "Create,Query,Update,Delete,Editing,ChangeTracking")
227 jView.move(folder = folder)
228 jViewFLC = arcgis.features.FeatureLayerCollection.fromitem(jView)
c:\Users\user\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone\lib\site-packages\arcgis\features\managers.py in create_view(self, name, spatial_reference, extent, allow_schema_changes, updateable, capabilities, view_layers, view_tables, description, tags, snippet, overwrite, set_item_id, preserve_layer_ids)
2006 params["description"] = description
2007 res = gis._con.post(path=url, postdata=params)
-> 2008 view = content.get(res["itemId"])
2009 fs_view = FeatureLayerCollection(url=view.url, gis=gis)
2010 add_def = {"layers": [], "tables": []}
KeyError: 'itemId'
The loop runs all the way through the first two items in the list and then fails on the third. It was running all the way through two days ago and nothing has much changed (I made some tweaks to other areas of the script, but don't think it would affect it). How do I isolate the error?
You need to look into try/except for error handling with Python. This will allow you to handle the error gracefully. You are getting a KeyError which means the "itemId" key was not found in your dictionary. With exceptions handling you can tell the script what to do when this error is encountered.