Skip to content

Commit

Permalink
fix invalid identifiers error code (#424)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewleap-optimizely authored Apr 25, 2023
1 parent a2dba60 commit 48347c5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
1 change: 0 additions & 1 deletion optimizely/helpers/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ class Errors:
NONE_VARIABLE_KEY_PARAMETER: Final = '"None" is an invalid value for variable key.'
UNSUPPORTED_DATAFILE_VERSION: Final = (
'This version of the Python SDK does not support the given datafile version: "{}".')
INVALID_SEGMENT_IDENTIFIER: Final = 'Audience segments fetch failed (invalid identifier).'
FETCH_SEGMENTS_FAILED: Final = 'Audience segments fetch failed ({}).'
ODP_EVENT_FAILED: Final = 'ODP event send failed ({}).'
ODP_NOT_INTEGRATED: Final = 'ODP is not integrated.'
Expand Down
12 changes: 7 additions & 5 deletions optimizely/odp/odp_segment_api_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,15 @@ def fetch_segments(self, api_key: str, api_host: str, user_key: str,

if response_dict and 'errors' in response_dict:
try:
error_class = response_dict['errors'][0]['extensions']['classification']
except (KeyError, IndexError):
extensions = response_dict['errors'][0]['extensions']
error_class = extensions['classification']
error_code = extensions.get('code')
except (KeyError, IndexError, TypeError):
self.logger.error(Errors.FETCH_SEGMENTS_FAILED.format('decode error'))
return None

if error_class == 'InvalidIdentifierException':
self.logger.warning(Errors.INVALID_SEGMENT_IDENTIFIER)
if error_code == 'INVALID_IDENTIFIER_EXCEPTION':
self.logger.warning(Errors.FETCH_SEGMENTS_FAILED.format('invalid identifier'))
return None
else:
self.logger.error(Errors.FETCH_SEGMENTS_FAILED.format(error_class))
Expand All @@ -188,6 +190,6 @@ def fetch_segments(self, api_key: str, api_host: str, user_key: str,
audiences = response_dict['data']['customer']['audiences']['edges']
segments = [edge['node']['name'] for edge in audiences if edge['node']['state'] == 'qualified']
return segments
except KeyError:
except (KeyError, TypeError):
self.logger.error(Errors.FETCH_SEGMENTS_FAILED.format('decode error'))
return None
3 changes: 2 additions & 1 deletion tests/test_odp_segment_api_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ def test_fetch_qualified_segments__500(self):
"customer"
],
"extensions": {
"classification": "InvalidIdentifierException"
"classification": "DataFetchingException",
"code": "INVALID_IDENTIFIER_EXCEPTION"
}
}
],
Expand Down

0 comments on commit 48347c5

Please sign in to comment.