'OktaAPIException' object has no attribute 'errorCode'

Hi,

I’m using python SDK and I can’t extract errorCode from ‘OktaAPIException’ object.

The code below is failing with AttributeError: ‘OktaAPIException’ object has no attribute ‘errorCode’:

try:
    loop.run_until_complete(some_function())
except OktaAPIException as error:
    print(error.errorCode)

error object is shown below:

{'errorCode': 'E0000001', 'errorSummary': 'Api validation failed: name', 'errorLink': 'E0000001', 'errorId': 'xxxxxxxxxxxxxxxx', 'errorCauses': [{'errorSummary': 'name: An object with this field already exists in the current organization'}]}

You should be able to retrieve that using the args parameter of the error object

For example,

except OktaAPIException as err:
       errDict = err.args[0]
       print(errDict['errorCode'])
       print(errDict['errorSummary'])
1 Like

Many thanks, that worked. I ended up with something too complicated, i.e.
ast.literal_eval(str(err))

Your solution is nice and elegant, thanks again!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.