Skip to content

new: raise_on_document_error for insert_many & delete_many #369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions arango/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1752,6 +1752,7 @@ def insert_many(
merge: Optional[bool] = None,
refill_index_caches: Optional[bool] = None,
version_attribute: Optional[str] = None,
raise_on_document_error: bool = False,
) -> Result[Union[bool, List[Union[Json, ArangoServerError]]]]:
"""Insert multiple documents.

Expand All @@ -1761,7 +1762,8 @@ def insert_many(
returned as an object in the result list. It is up to you to
inspect the list to determine which documents were inserted
successfully (returns document metadata) and which were not
(returns exception object).
(returns exception object). Alternatively, you can rely on
setting **raise_on_document_error** to True (defaults to False).

:param documents: List of new documents to insert. If they contain the
"_key" or "_id" fields, the values are used as the keys of the new
Expand Down Expand Up @@ -1801,6 +1803,11 @@ def insert_many(
:param version_attribute: support for simple external versioning to
document operations.
:type version_attribute: str
:param raise_on_document_error: Whether to raise if a DocumentRevisionError
or a DocumentInsertError is encountered on an individual document,
as opposed to returning the error as an object in the result list.
Defaults to False.
:type raise_on_document_error: bool
:return: List of document metadata (e.g. document keys, revisions) and
any exception, or True if parameter **silent** was set to True.
:rtype: [dict | ArangoServerError] | bool
Expand Down Expand Up @@ -1853,7 +1860,12 @@ def response_handler(
results.append(body)
else:
sub_resp = self._conn.prep_bulk_err_response(resp, body)
results.append(DocumentInsertError(sub_resp, request))
error = DocumentInsertError(sub_resp, request)

if raise_on_document_error:
raise error

results.append(error)

return results

Expand Down Expand Up @@ -2228,6 +2240,7 @@ def delete_many(
sync: Optional[bool] = None,
silent: bool = False,
refill_index_caches: Optional[bool] = None,
raise_on_document_error: bool = False,
) -> Result[Union[bool, List[Union[Json, ArangoServerError]]]]:
"""Delete multiple documents.

Expand Down Expand Up @@ -2256,6 +2269,11 @@ def delete_many(
index caches if document operations affect the edge index or
cache-enabled persistent indexes.
:type refill_index_caches: bool | None
:param raise_on_document_error: Whether to raise if a DocumentRevisionError
or a DocumentDeleteError is encountered on an individual document,
as opposed to returning the error as an object in the result list.
Defaults to False.
:type raise_on_document_error: bool
:return: List of document metadata (e.g. document keys, revisions) and
any exceptions, or True if parameter **silent** was set to True.
:rtype: [dict | ArangoServerError] | bool
Expand Down Expand Up @@ -2307,6 +2325,10 @@ def response_handler(
error = DocumentRevisionError(sub_resp, request)
else:
error = DocumentDeleteError(sub_resp, request)

if raise_on_document_error:
raise error

results.append(error)

return results
Expand Down
12 changes: 12 additions & 0 deletions tests/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ def test_document_insert_many(col, bad_col, docs):
assert isinstance(result["old"], dict)
assert isinstance(result["_old_rev"], str)

# Test insert_many with raise_on_document_error set to True
with assert_raises(DocumentInsertError) as err:
col.insert_many(docs, raise_on_document_error=True)

# Test get with bad database
with assert_raises(DocumentInsertError) as err:
bad_col.insert_many(docs)
Expand Down Expand Up @@ -1092,6 +1096,10 @@ def test_document_delete_many(col, bad_col, docs):
assert "[HTTP 202][ERR 1200]" in error.message
assert len(col) == 6

# Test delete_many with raise_on_document_error set to True
with assert_raises(DocumentRevisionError) as err:
col.delete_many(docs, raise_on_document_error=True)

# Test delete_many (documents) with missing documents
empty_collection(col)
results = col.delete_many(
Expand All @@ -1109,6 +1117,10 @@ def test_document_delete_many(col, bad_col, docs):
assert "[HTTP 202][ERR 1202]" in error.message
assert len(col) == 0

# Test delete_many with raise_on_document_error set to True
with assert_raises(DocumentDeleteError) as err:
col.delete_many(docs, raise_on_document_error=True)

# Test delete_many with bad database
with assert_raises(DocumentDeleteError) as err:
bad_col.delete_many(docs)
Expand Down
Loading