Esri / ArcREST

python package for REST API (AGS, AGOL, webmap JSON, etc..)
Apache License 2.0
192 stars 155 forks source link

Fix issue #282 - "KeyError: 'http'" exception #283

Closed sloanlance closed 7 years ago

sloanlance commented 7 years ago

TL;DR

Check for 'http' key and return None if not found. Also removed some redundancies and improved readability.

Resolves #282: Mysterious "'http'" printed for unknown reason

Details

When running my program, the message 'http' appears in the output with no apparent source at first. After debugging, I found it was an exception being caught on line 336 of arcresthelper.securityhandlerhelper.securityhandlerhelper.__init__(). Commenting out this except block, I found that this exception was being thrown:

arcresthelper.common.ArcRestHelperError: {
'function': 'securityhandlerhelper_init', 
'line': 'line 298', 
'synerror': "KeyError: 'http'", 
'filename': '/…/ArcREST/src/arcresthelper/securityhandlerhelper.py'}

Line 298 contains a call to arcrest.manageorg._portals.Portal.featureServers(), which on line 992 of that file, attempts to access the 'http' key without checking for it first:

            res = urls['urls']['features']['http']

The code in Portal.featureServers() is only called by securityhandlerhelper.__init__(), which checks to see whether the return value is None before using it. A safe way to prevent the exception is to check for the 'http' key with a conditional before using it and return None if that fails.

I also removed some repeated code, since that's a common place for mistakes to appear later, and reorganized and reformatted this method to improve readability.

achapkowski commented 7 years ago

@MikeMillerGIS can you verify this pull request?

sloanlance commented 7 years ago

Thanks, @achapkowski. Let me know if you have questions, @MikeMillerGIS.

sloanlance commented 7 years ago

Thanks, @MikeMillerGIS.