跳到主要内容

Zotero 文献库

收藏0

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

让 AI 安装

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

references/error-handling.md

3.9 KB查看源码

Error Handling#

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.

ExceptionMeaning
PyZoteroErrorBase class for SDK errors
UserNotAuthorisedError401/403, invalid key or insufficient permissions
HTTPErrorOther mapped HTTP error
ParamNotPassedErrorMissing method argument/data
CallDoesNotExistErrorUnsupported method for this library/mode
ResourceNotFoundError404, missing resource or missing full-text content
ConflictError409, target library locked
PreConditionFailedError412, stale version or reused write token
TooManyItemsErrorSDK batch-size check, normally over 50
InvalidItemFieldsErrorUnknown field names
TooManyRetriesErrorSDK retries exhausted
LocalAPIKeyRequiredErrorLocal write needs a valid local key
LocalAPIDeniedErrorUser declined local authorization
ServerIDMismatchErrorLocal database differs from cached server ID
ServerIDRequiredErrorLocal write missing server-ID precondition

Read Errors#

import httpx2
from pyzotero.errors import ResourceNotFoundError, UserNotAuthorisedError

try:
    item = zot.item('ITEMKEY')
except ResourceNotFoundError:
    print('Item not found')
except UserNotAuthorisedError:
    print('Check the key and library permissions')
except httpx2.TransportError:
    print('Network request failed; retry the read when connectivity returns')

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).

Version Conflicts#

from pyzotero.errors import PreConditionFailedError, ServerIDMismatchError

try:
    zot.update_item(item)
except ServerIDMismatchError:
    # Subclass of PreConditionFailedError: handle it first.
    raise RuntimeError('Local Zotero database changed; discard cached objects and versions')
except PreConditionFailedError:
    fresh_item = zot.item(item['key'])
    # Compare the original, intended, and fresh values before merging.
    # Do not automatically overwrite a concurrent edit with new_title.
    raise RuntimeError('Concurrent edit detected; reconcile before another write')

Invalid Fields#

from pyzotero.errors import InvalidItemFieldsError

template = zot.item_template('journalArticle')  # remote API only
template['badField'] = 'bad value'
try:
    zot.check_items([template])
except InvalidItemFieldsError:
    print('Remove unknown fields before creating the item')

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.

Rate Limits and Retry Boundaries#

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.

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.

Sources: Zotero backoff rules, Pyzotero error implementation.

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