Skip to content

Commit c6e923b

Browse files
authored
new: raise_on_document_error for insert_many & delete_many (#369)
1 parent 6e5f8f1 commit c6e923b

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

arango/collection.py

+24-2
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,7 @@ def insert_many(
17521752
merge: Optional[bool] = None,
17531753
refill_index_caches: Optional[bool] = None,
17541754
version_attribute: Optional[str] = None,
1755+
raise_on_document_error: bool = False,
17551756
) -> Result[Union[bool, List[Union[Json, ArangoServerError]]]]:
17561757
"""Insert multiple documents.
17571758
@@ -1761,7 +1762,8 @@ def insert_many(
17611762
returned as an object in the result list. It is up to you to
17621763
inspect the list to determine which documents were inserted
17631764
successfully (returns document metadata) and which were not
1764-
(returns exception object).
1765+
(returns exception object). Alternatively, you can rely on
1766+
setting **raise_on_document_error** to True (defaults to False).
17651767
17661768
:param documents: List of new documents to insert. If they contain the
17671769
"_key" or "_id" fields, the values are used as the keys of the new
@@ -1801,6 +1803,11 @@ def insert_many(
18011803
:param version_attribute: support for simple external versioning to
18021804
document operations.
18031805
:type version_attribute: str
1806+
:param raise_on_document_error: Whether to raise if a DocumentRevisionError
1807+
or a DocumentInsertError is encountered on an individual document,
1808+
as opposed to returning the error as an object in the result list.
1809+
Defaults to False.
1810+
:type raise_on_document_error: bool
18041811
:return: List of document metadata (e.g. document keys, revisions) and
18051812
any exception, or True if parameter **silent** was set to True.
18061813
:rtype: [dict | ArangoServerError] | bool
@@ -1853,7 +1860,12 @@ def response_handler(
18531860
results.append(body)
18541861
else:
18551862
sub_resp = self._conn.prep_bulk_err_response(resp, body)
1856-
results.append(DocumentInsertError(sub_resp, request))
1863+
error = DocumentInsertError(sub_resp, request)
1864+
1865+
if raise_on_document_error:
1866+
raise error
1867+
1868+
results.append(error)
18571869

18581870
return results
18591871

@@ -2228,6 +2240,7 @@ def delete_many(
22282240
sync: Optional[bool] = None,
22292241
silent: bool = False,
22302242
refill_index_caches: Optional[bool] = None,
2243+
raise_on_document_error: bool = False,
22312244
) -> Result[Union[bool, List[Union[Json, ArangoServerError]]]]:
22322245
"""Delete multiple documents.
22332246
@@ -2256,6 +2269,11 @@ def delete_many(
22562269
index caches if document operations affect the edge index or
22572270
cache-enabled persistent indexes.
22582271
:type refill_index_caches: bool | None
2272+
:param raise_on_document_error: Whether to raise if a DocumentRevisionError
2273+
or a DocumentDeleteError is encountered on an individual document,
2274+
as opposed to returning the error as an object in the result list.
2275+
Defaults to False.
2276+
:type raise_on_document_error: bool
22592277
:return: List of document metadata (e.g. document keys, revisions) and
22602278
any exceptions, or True if parameter **silent** was set to True.
22612279
:rtype: [dict | ArangoServerError] | bool
@@ -2307,6 +2325,10 @@ def response_handler(
23072325
error = DocumentRevisionError(sub_resp, request)
23082326
else:
23092327
error = DocumentDeleteError(sub_resp, request)
2328+
2329+
if raise_on_document_error:
2330+
raise error
2331+
23102332
results.append(error)
23112333

23122334
return results

tests/test_document.py

+12
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ def test_document_insert_many(col, bad_col, docs):
239239
assert isinstance(result["old"], dict)
240240
assert isinstance(result["_old_rev"], str)
241241

242+
# Test insert_many with raise_on_document_error set to True
243+
with assert_raises(DocumentInsertError) as err:
244+
col.insert_many(docs, raise_on_document_error=True)
245+
242246
# Test get with bad database
243247
with assert_raises(DocumentInsertError) as err:
244248
bad_col.insert_many(docs)
@@ -1092,6 +1096,10 @@ def test_document_delete_many(col, bad_col, docs):
10921096
assert "[HTTP 202][ERR 1200]" in error.message
10931097
assert len(col) == 6
10941098

1099+
# Test delete_many with raise_on_document_error set to True
1100+
with assert_raises(DocumentRevisionError) as err:
1101+
col.delete_many(docs, raise_on_document_error=True)
1102+
10951103
# Test delete_many (documents) with missing documents
10961104
empty_collection(col)
10971105
results = col.delete_many(
@@ -1109,6 +1117,10 @@ def test_document_delete_many(col, bad_col, docs):
11091117
assert "[HTTP 202][ERR 1202]" in error.message
11101118
assert len(col) == 0
11111119

1120+
# Test delete_many with raise_on_document_error set to True
1121+
with assert_raises(DocumentDeleteError) as err:
1122+
col.delete_many(docs, raise_on_document_error=True)
1123+
11121124
# Test delete_many with bad database
11131125
with assert_raises(DocumentDeleteError) as err:
11141126
bad_col.delete_many(docs)

0 commit comments

Comments
 (0)