Skip to content

Commit 5f01703

Browse files
committed
PYTHON-2560 Add more info to the KMS failed error
1 parent 8455689 commit 5f01703

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

pymongo/asynchronous/encryption.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,6 @@ async def kms_request(self, kms_context: MongoCryptKmsContext) -> None:
199199
if not data:
200200
raise OSError("KMS connection closed")
201201
kms_context.feed(data)
202-
# Async raises an OSError instead of returning empty bytes
203-
except OSError as err:
204-
raise OSError("KMS connection closed") from err
205202
except BLOCKING_IO_ERRORS:
206203
raise socket.timeout("timed out") from None
207204
finally:
@@ -216,7 +213,13 @@ async def kms_request(self, kms_context: MongoCryptKmsContext) -> None:
216213
# Wrap I/O errors in PyMongo exceptions.
217214
_raise_connection_failure((host, port), exc)
218215
# Mark this attempt as failed and defer to libmongocrypt to retry.
219-
kms_context.fail()
216+
try:
217+
kms_context.fail()
218+
except MongoCryptError as final_err:
219+
exc = MongoCryptError(
220+
f"{final_err}, last attempt failed with: {exc}", final_err.code
221+
)
222+
raise exc from final_err
220223

221224
async def collection_info(self, database: str, filter: bytes) -> Optional[bytes]:
222225
"""Get the collection info for a namespace.

pymongo/synchronous/encryption.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,6 @@ def kms_request(self, kms_context: MongoCryptKmsContext) -> None:
199199
if not data:
200200
raise OSError("KMS connection closed")
201201
kms_context.feed(data)
202-
# Async raises an OSError instead of returning empty bytes
203-
except OSError as err:
204-
raise OSError("KMS connection closed") from err
205202
except BLOCKING_IO_ERRORS:
206203
raise socket.timeout("timed out") from None
207204
finally:
@@ -216,7 +213,13 @@ def kms_request(self, kms_context: MongoCryptKmsContext) -> None:
216213
# Wrap I/O errors in PyMongo exceptions.
217214
_raise_connection_failure((host, port), exc)
218215
# Mark this attempt as failed and defer to libmongocrypt to retry.
219-
kms_context.fail()
216+
try:
217+
kms_context.fail()
218+
except MongoCryptError as final_err:
219+
exc = MongoCryptError(
220+
f"{final_err}, last attempt failed with: {exc}", final_err.code
221+
)
222+
raise exc from final_err
220223

221224
def collection_info(self, database: str, filter: bytes) -> Optional[bytes]:
222225
"""Get the collection info for a namespace.

0 commit comments

Comments
 (0)