跳到主要内容

Zotero 文献库

收藏0

用 pyzotero 程序化管理 Zotero 文献库:检索条目、新建与更新条目、整理分类与标签、导出引用格式、上传 PDF 附件,支持 Web API 与本地 API。

让 AI 安装

星光尘缘LV2最新版本 0.1.0更新于 2026-11-24下载 0 次

references/error-handling.md

3.9 KB查看排版效果
1# Error Handling
2
3Pyzotero 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
26import httpx2
27from pyzotero.errors import ResourceNotFoundError, UserNotAuthorisedError
28
29try:
30 item = zot.item('ITEMKEY')
31except ResourceNotFoundError:
32 print('Item not found')
33except UserNotAuthorisedError:
34 print('Check the key and library permissions')
35except httpx2.TransportError:
36 print('Network request failed; retry the read when connectivity returns')
37```
38
39Many 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
44from pyzotero.errors import PreConditionFailedError, ServerIDMismatchError
45
46try:
47 zot.update_item(item)
48except ServerIDMismatchError:
49 # Subclass of PreConditionFailedError: handle it first.
50 raise RuntimeError('Local Zotero database changed; discard cached objects and versions')
51except 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
61from pyzotero.errors import InvalidItemFieldsError
62
63template = zot.item_template('journalArticle') # remote API only
64template['badField'] = 'bad value'
65try:
66 zot.check_items([template])
67except InvalidItemFieldsError:
68 print('Remove unknown fields before creating the item')
69```
70
71This 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
75Honor `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
77Do 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
79Sources: [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).

文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。