Skip to content

Commit 674a9a7

Browse files
author
Abel Milash
committed
Fix _validate_guid to catch TypeError for None/int GUID inputs
1 parent 30a349b commit 674a9a7

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

src/PowerPlatform/Dataverse/operations/records.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,19 @@
2121

2222

2323
def _validate_guid(record_id: str, param_name: str = "record_id") -> None:
24-
"""Raise ValueError if *record_id* is not a valid GUID."""
24+
"""Raise ValueError if *record_id* is not a valid GUID.
25+
26+
Catches all non-GUID inputs uniformly:
27+
* ``ValueError`` -- malformed hex string (``uuid.UUID("not-a-guid")``)
28+
* ``AttributeError`` -- input lacks ``replace``/``encode`` (rare)
29+
* ``TypeError`` -- non-string input such as ``None`` or ``int``
30+
(``uuid.UUID(None)`` / ``uuid.UUID(123)``). Without this, callers
31+
would see the raw constructor message ``"one of the hex, bytes, ...
32+
arguments must be given"`` instead of the helpful message below.
33+
"""
2534
try:
2635
uuid.UUID(record_id)
27-
except (ValueError, AttributeError):
36+
except (ValueError, AttributeError, TypeError):
2837
raise ValueError(
2938
f"{param_name} {record_id!r} is not a valid GUID "
3039
f"(expected 8-4-4-4-12 hex, e.g. 'a1b2c3d4-1234-5678-9abc-def012345678'). "

tests/unit/test_records_operations.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,20 @@ def test_valid_guid_accepted(self):
202202
result = self.client.records.retrieve("account", "a1b2c3d4-1234-5678-9abc-def012345678")
203203
self.assertIsNotNone(result)
204204

205+
def test_retrieve_none_record_id_raises_value_error(self):
206+
"""retrieve(None) surfaces the friendly ValueError, not the raw uuid TypeError."""
207+
with self.assertRaises(ValueError) as ctx:
208+
self.client.records.retrieve("account", None)
209+
self.assertIn("not a valid GUID", str(ctx.exception))
210+
self.client._odata._get.assert_not_called()
211+
212+
def test_retrieve_int_record_id_raises_value_error(self):
213+
"""retrieve(<int>) surfaces the friendly ValueError, not the raw uuid TypeError."""
214+
with self.assertRaises(ValueError) as ctx:
215+
self.client.records.retrieve("account", 12345)
216+
self.assertIn("not a valid GUID", str(ctx.exception))
217+
self.client._odata._get.assert_not_called()
218+
205219
# --------------------------------------------------------------------- get
206220

207221
def test_get_single(self):

0 commit comments

Comments
 (0)