Skip to content

Commit 115d124

Browse files
MichaelGHSegclaude
andcommitted
Address PR review: catch KeyError in response parsing
- Add KeyError to except clause when parsing JSON response to handle missing 'code' or 'message' keys - Add explanatory comment on pre-existing except-pass pattern Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 2165298 commit 115d124

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

segment/analytics/request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def post(write_key, host=None, gzip=False, timeout=15, proxies=None, oauth_manag
100100
payload = res.json()
101101
log.debug('received response: %s', payload)
102102
raise APIError(res.status_code, payload['code'], payload['message'], res)
103-
except ValueError:
103+
except (ValueError, KeyError):
104104
log.error('Unknown error: [%s] %s', res.status_code, res.reason)
105105
raise APIError(res.status_code, 'unknown', res.text, res)
106106

segment/analytics/test/test_consumer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def test_request_retry(self):
155155
try:
156156
self._test_request_retry(consumer, api_error, 1)
157157
except APIError:
158-
pass
158+
pass # Expected: 400 is non-retryable, so the error propagates here
159159
else:
160160
self.fail('request() should not retry on client errors')
161161

0 commit comments

Comments
 (0)