Skip to content

Commit fd9e827

Browse files
Merge pull request #44 from UiPath/fix/error-serialization
fix: handle errors in serialization
2 parents 895af83 + 04dafc7 commit fd9e827

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "uipath-core"
3-
version = "0.5.0"
3+
version = "0.5.1"
44
description = "UiPath Core abstractions"
55
readme = { file = "README.md", content-type = "text/markdown" }
66
requires-python = ">=3.11"

src/uipath/core/serialization/json.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ def serialize_defaults(
8686
if hasattr(obj, "to_dict") and not isinstance(obj, type):
8787
return obj.to_dict()
8888

89+
# Handle objects with as_dict property (UiPathBaseRuntimeError)
90+
if hasattr(obj, "as_dict") and not isinstance(obj, type):
91+
return obj.as_dict
92+
8993
# Handle dataclasses
9094
if is_dataclass(obj) and not isinstance(obj, type):
9195
return asdict(obj)
@@ -107,6 +111,10 @@ def serialize_defaults(
107111
# Convert to list
108112
return list(obj)
109113

114+
# Handle exceptions
115+
if isinstance(obj, Exception):
116+
return str(obj)
117+
110118
# Handle datetime objects
111119
if isinstance(obj, datetime):
112120
return obj.isoformat()

tests/serialization/test_json.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,20 @@ def test_serializes_named_tuple(self) -> None:
268268
assert isinstance(parsed["point"], list)
269269
assert parsed["point"] == [10, 20]
270270

271+
def test_serializes_object_with_as_dict(self) -> None:
272+
"""Test object with as_dict property via json.dumps."""
273+
274+
class RuntimeLike:
275+
@property
276+
def as_dict(self) -> dict[str, Any]:
277+
return {"host": "localhost", "port": 8080}
278+
279+
obj = RuntimeLike()
280+
data = {"runtime": obj}
281+
result = serialize_json(data)
282+
parsed = json.loads(result)
283+
assert parsed["runtime"] == {"host": "localhost", "port": 8080}
284+
271285
def test_serializes_object_with_to_dict(self) -> None:
272286
"""Test object with to_dict method via json.dumps."""
273287

@@ -294,6 +308,14 @@ def __str__(self) -> str:
294308
parsed = json.loads(result)
295309
assert parsed["obj"] == "custom_string"
296310

311+
def test_serializes_exception(self) -> None:
312+
"""Test Exception serialization via json.dumps."""
313+
err = ValueError("something went wrong")
314+
data = {"error": err}
315+
result = serialize_json(data)
316+
parsed = json.loads(result)
317+
assert parsed["error"] == "something went wrong"
318+
297319
def test_with_json_dumps(self) -> None:
298320
"""Test integration with json.dumps()."""
299321

@@ -325,6 +347,7 @@ def test_with_json_dumps_complex_nested(self) -> None:
325347
"datetime": datetime(2024, 1, 1),
326348
"set": {1, 2, 3},
327349
"tuple": (4, 5, 6),
350+
"error": ValueError("something failed"),
328351
}
329352

330353
result = serialize_json(data)
@@ -336,6 +359,7 @@ def test_with_json_dumps_complex_nested(self) -> None:
336359
assert "2024-01-01" in parsed["datetime"]
337360
assert set(parsed["set"]) == {1, 2, 3}
338361
assert parsed["tuple"] == [4, 5, 6]
362+
assert parsed["error"] == "something failed"
339363

340364
def test_with_list_of_pydantic_models(self) -> None:
341365
"""Test with list of Pydantic models (common MCP scenario)."""

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)