Skip to content

Commit

Permalink
Python API: Improve error handling for messages that failed to parse
Browse files Browse the repository at this point in the history
  • Loading branch information
bglw committed Sep 29, 2024
1 parent 982708c commit a53f3c2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Python API > Pagefind error handling
platforms:
- linux
- mac

steps:
- ref: ./background.toolproof.yml
- step: I have a "public/run.py" file with the content {python}
python: |2-
import sys
sys.path.append('%repo_wd%/wrappers/python/src')
import asyncio
import json
import logging
import os
from pagefind.index import PagefindIndex, IndexConfig
async def main():
async with PagefindIndex() as index:
await index.delete_index()
try:
files = await index.get_files()
except AssertionError:
print("Index deleted")
try:
async with PagefindIndex(IndexConfig(root_selector=5)) as index:
await index.delete_index()
except Exception as e:
print(f"Caught error {e}")
print("Complete")
if __name__ == "__main__":
asyncio.run(main())
- step: I run "cd public && PAGEFIND_BINARY_PATH=%pagefind_exec_path% python3 run.py"
- step: 'stdout should contain "invalid type: integer `5`"'
- step: stdout should contain "Index deleted"
9 changes: 8 additions & 1 deletion wrappers/python/src/pagefind/service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,14 @@ async def _wait_for_responses(self) -> None:
if (resp := json.loads(base64.b64decode(output[:-1]))) is None:
continue
resp = cast(InternalServiceResponse, resp)
if (message_id := resp.get("message_id")) is not None:
message_id = resp.get("message_id")
if message_id is None:
# If the backend service failed to parse the message, it won't return the ID
# However it does return the message itself, so we can retrieve the ID we sent
if (orginal := resp["payload"].get("original_message")) is not None:
if (sent := json.loads(orginal)) is not None:
message_id = sent.get("message_id")
if message_id is not None:
log.debug(f"received response for message {message_id}")
assert (
self._message_id >= message_id
Expand Down

2 comments on commit a53f3c2

@SKalt
Copy link
Contributor

@SKalt SKalt commented on a53f3c2 Sep 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code golf: you can keep the if (message_id := resp.get("message_id") is None: ...; else: ... structure; the variable created by the walrus operator will be available in both branches.
Here's an example:

a = {"a": 1}
if (b := a.get("a")) is None:
    ...
else:
    print(f"b is {b}")

@bglw
Copy link
Contributor Author

@bglw bglw commented on a53f3c2 Sep 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm not sure if I follow in context — I want the main if branch to run if there is a message_id at either the root or in the original message, so was avoiding duplicating some of the future logic into an else: branch.

Might be missing something though! I'm not particularly fluent here 😄 Feel free to jump in and clobber over anything I have done! This handling is specifically covered by one of the Toolproof tests so as long as that passes I'm happy ☺

Please sign in to comment.