references/error-handling.md
3.9 KB查看排版效果| 1 | # Error Handling |
| 2 | |
| 3 | Pyzotero 1.15.2 exports exceptions from `pyzotero.errors` (also from `pyzotero`). Use the current `Error` suffixes; old names such as `ResourceNotFound` do not exist. |
| 4 | |
| 5 | | Exception | Meaning | |
| 6 | |-----------|---------| |
| 7 | | `PyZoteroError` | Base class for SDK errors | |
| 8 | | `UserNotAuthorisedError` | 401/403, invalid key or insufficient permissions | |
| 9 | | `HTTPError` | Other mapped HTTP error | |
| 10 | | `ParamNotPassedError` | Missing method argument/data | |
| 11 | | `CallDoesNotExistError` | Unsupported method for this library/mode | |
| 12 | | `ResourceNotFoundError` | 404, missing resource or missing full-text content | |
| 13 | | `ConflictError` | 409, target library locked | |
| 14 | | `PreConditionFailedError` | 412, stale version or reused write token | |
| 15 | | `TooManyItemsError` | SDK batch-size check, normally over 50 | |
| 16 | | `InvalidItemFieldsError` | Unknown field names | |
| 17 | | `TooManyRetriesError` | SDK retries exhausted | |
| 18 | | `LocalAPIKeyRequiredError` | Local write needs a valid local key | |
| 19 | | `LocalAPIDeniedError` | User declined local authorization | |
| 20 | | `ServerIDMismatchError` | Local database differs from cached server ID | |
| 21 | | `ServerIDRequiredError` | Local write missing server-ID precondition | |
| 22 | |
| 23 | ## Read Errors |
| 24 | |
| 25 | ```python |
| 26 | import httpx2 |
| 27 | from pyzotero.errors import ResourceNotFoundError, UserNotAuthorisedError |
| 28 | |
| 29 | try: |
| 30 | item = zot.item('ITEMKEY') |
| 31 | except ResourceNotFoundError: |
| 32 | print('Item not found') |
| 33 | except UserNotAuthorisedError: |
| 34 | print('Check the key and library permissions') |
| 35 | except httpx2.TransportError: |
| 36 | print('Network request failed; retry the read when connectivity returns') |
| 37 | ``` |
| 38 | |
| 39 | Many mapped errors retain the HTTP exception in `__cause__`. Inspect response status and headers when needed, but redact credentials before logging request URLs or full exception bodies (`key_info()` embeds the key in its path). |
| 40 | |
| 41 | ## Version Conflicts |
| 42 | |
| 43 | ```python |
| 44 | from pyzotero.errors import PreConditionFailedError, ServerIDMismatchError |
| 45 | |
| 46 | try: |
| 47 | zot.update_item(item) |
| 48 | except ServerIDMismatchError: |
| 49 | # Subclass of PreConditionFailedError: handle it first. |
| 50 | raise RuntimeError('Local Zotero database changed; discard cached objects and versions') |
| 51 | except PreConditionFailedError: |
| 52 | fresh_item = zot.item(item['key']) |
| 53 | # Compare the original, intended, and fresh values before merging. |
| 54 | # Do not automatically overwrite a concurrent edit with new_title. |
| 55 | raise RuntimeError('Concurrent edit detected; reconcile before another write') |
| 56 | ``` |
| 57 | |
| 58 | ## Invalid Fields |
| 59 | |
| 60 | ```python |
| 61 | from pyzotero.errors import InvalidItemFieldsError |
| 62 | |
| 63 | template = zot.item_template('journalArticle') # remote API only |
| 64 | template['badField'] = 'bad value' |
| 65 | try: |
| 66 | zot.check_items([template]) |
| 67 | except InvalidItemFieldsError: |
| 68 | print('Remove unknown fields before creating the item') |
| 69 | ``` |
| 70 | |
| 71 | This validation does not replace checking `failed` in batch write JSON. HTTP 200 and `update_items()` returning `True` do not establish that every object was updated. |
| 72 | |
| 73 | ## Rate Limits and Retry Boundaries |
| 74 | |
| 75 | Honor `Backoff` even on successful responses, and `Retry-After` on 429/503. Pyzotero's read and decorated Boolean-write paths record server delays and retry 429 responses, eventually raising `TooManyRetriesError`. Some other paths, including `create_items()`, `saved_search()`, and `new_fulltext()`, do not run that retry loop in 1.15.2: inspect the returned response/`zot.request` where applicable, and do not interpret an error payload as a valid result. |
| 76 | |
| 77 | Do not wrap all calls in a short fixed exponential retry: it can violate server delays or repeat writes whose outcome is unknown. Reconcile timeouts before replaying creates/uploads, and retry only known failures. `TooManyRequestsError` exists but is not the ordinary terminal error from the SDK's 429 retry loop. Do not retry local authorization dialogs automatically. |
| 78 | |
| 79 | Sources: [Zotero backoff rules](https://www.zotero.org/support/dev/web_api/v3/basics#rate_limiting), [Pyzotero error implementation](https://github.com/urschrei/pyzotero/blob/v1.15.2/src/pyzotero/errors.py). |
文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。

