Skip to content

Commit 299103a

Browse files
digithreeclaude
andcommitted
Fix KeyError when API response missing 'list' or 'since' keys
- Replace dict access with .get() method to handle missing keys gracefully - Add comprehensive tests for both missing 'list' and 'since' key scenarios - Prevents crashes when Pocket API returns unexpected response format Fixes the stack trace: KeyError: 'list' at utils.py line 119 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent fd5f2ac commit 299103a

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

pocket_to_sqlite/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ def __iter__(self):
116116
retries = 0
117117
response.raise_for_status()
118118
page = response.json()
119-
items = list((page["list"] or {}).values())
120-
next_since = page["since"]
119+
items = list((page.get("list") or {}).values())
120+
next_since = page.get("since")
121121
if self.record_since and next_since:
122122
self.record_since(next_since)
123123
if not items:

tests/test_save_pocket.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,51 @@ def test_authors(converted):
7272
"url": "http://people.idsia.ch/~juergen/heatexchanger/heatexchanger.html",
7373
}
7474
] == authors
75+
76+
77+
def test_fetch_items_handles_missing_list_key():
78+
"""Test that FetchItems handles API responses missing the 'list' key gracefully."""
79+
from unittest.mock import Mock, patch
80+
81+
# Mock auth
82+
auth = {
83+
"pocket_consumer_key": "test_key",
84+
"pocket_access_token": "test_token"
85+
}
86+
87+
# Mock response without 'list' key
88+
mock_response = Mock()
89+
mock_response.status_code = 200
90+
mock_response.json.return_value = {"since": 1234567890} # Missing 'list' key
91+
mock_response.raise_for_status.return_value = None
92+
93+
with patch('requests.get', return_value=mock_response):
94+
fetcher = utils.FetchItems(auth, page_size=1)
95+
items = list(fetcher)
96+
97+
# Should return empty list instead of raising KeyError
98+
assert items == []
99+
100+
101+
def test_fetch_items_handles_missing_since_key():
102+
"""Test that FetchItems handles API responses missing the 'since' key gracefully."""
103+
from unittest.mock import Mock, patch
104+
105+
# Mock auth
106+
auth = {
107+
"pocket_consumer_key": "test_key",
108+
"pocket_access_token": "test_token"
109+
}
110+
111+
# Mock response without 'since' key
112+
mock_response = Mock()
113+
mock_response.status_code = 200
114+
mock_response.json.return_value = {"list": {}} # Missing 'since' key
115+
mock_response.raise_for_status.return_value = None
116+
117+
with patch('requests.get', return_value=mock_response):
118+
fetcher = utils.FetchItems(auth, page_size=1)
119+
items = list(fetcher)
120+
121+
# Should handle missing 'since' key gracefully
122+
assert items == []

0 commit comments

Comments
 (0)