Skip to content

Commit c4fb76b

Browse files
committed
Improve exception handling: Properly raise IntegrityError exceptions
... when receiving `DuplicateKeyException` errors from CrateDB.
1 parent c276159 commit c4fb76b

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Unreleased
1313
certificate details are immanent, like no longer accepting the long
1414
deprecated ``commonName`` attribute. Instead, going forward, only the
1515
``subjectAltName`` attribute will be used.
16+
- DBAPI: Properly raise ``IntegrityError`` exceptions instead of
17+
``ProgrammingError``, when CrateDB raises a ``DuplicateKeyException``.
1618

1719
.. _urllib3 v2.0 migration guide: https://urllib3.readthedocs.io/en/latest/v2-migration-guide.html
1820
.. _urllib3 v2.0 roadmap: https://urllib3.readthedocs.io/en/stable/v2-roadmap.html

src/crate/client/http.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
BlobLocationNotFoundException,
5757
DigestNotFoundException,
5858
ProgrammingError,
59+
IntegrityError,
5960
)
6061

6162

@@ -191,6 +192,18 @@ def _ex_to_message(ex):
191192

192193

193194
def _raise_for_status(response):
195+
"""
196+
Properly raise `IntegrityError` exceptions for CrateDB's `DuplicateKeyException` errors.
197+
"""
198+
try:
199+
return _raise_for_status_real(response)
200+
except ProgrammingError as ex:
201+
if "DuplicateKeyException" in ex.message:
202+
raise IntegrityError(ex.message, error_trace=ex.error_trace) from ex
203+
raise
204+
205+
206+
def _raise_for_status_real(response):
194207
""" make sure that only crate.exceptions are raised that are defined in
195208
the DB-API specification """
196209
message = ''

0 commit comments

Comments
 (0)