deta-python icon indicating copy to clipboard operation
deta-python copied to clipboard

Fix handling of failed items in `Base.put`

Open lemonyte opened this issue 3 years ago • 0 comments

The Problem

The put method would raise a KeyError when trying to submit a dictionary. Issue originally brought up in https://github.com/deta/docs/discussions/431.

The Cause

The data dictionary that was submitted had empty keys (i.e. {"": "foo"}), which the API rejected and returned in a response like the following:

{
    "failed": {
        "items": [
            {"": "foo", "detabase_id": "<id>", "key": "<key>"}
        ]
    }
}

The put method was expecting a "processed" key to be present, and consequently failing when no such key existed. https://github.com/deta/deta-python/blob/49d53b8aa193f59a1085928cecf428710c1c69fa/deta/base.py#L161

The Solution

The put method now checks if a "processed" exists before accessing it. If it does not exist, it handles the response as any other unsuccessful response, and returns None.


Changes

  • Base.put will no longer throw a KeyError when a PUT request fails and the API returns a response with the failed field
  • Added workaround when raising urllib.error.HTTPError so the traceback prints properly
  • Fixed TTL tests

Additional

Perhaps it would be a good idea to make sure the API always returns a "processed" key, which would be empty when all items failed to add?

lemonyte avatar Aug 31 '22 03:08 lemonyte