|
| 1 | +"""Tests for raise_for_enriched helper.""" |
| 2 | + |
| 3 | +import json |
| 4 | + |
| 5 | +import httpx |
| 6 | +import pytest |
| 7 | +from uipath.platform.errors import EnrichedException |
| 8 | +from uipath.runtime.errors import UiPathErrorCategory |
| 9 | + |
| 10 | +from uipath_langchain.agent.exceptions import AgentRuntimeError, AgentRuntimeErrorCode |
| 11 | +from uipath_langchain.agent.exceptions.helpers import raise_for_enriched |
| 12 | + |
| 13 | + |
| 14 | +def _make_enriched( |
| 15 | + status: int, |
| 16 | + body: dict[str, object] | None = None, |
| 17 | + url: str = "https://cloud.uipath.com/org/tenant/orchestrator_/api/v1", |
| 18 | +) -> EnrichedException: |
| 19 | + content = json.dumps(body).encode() if body else b"" |
| 20 | + request = httpx.Request("POST", url) |
| 21 | + response = httpx.Response( |
| 22 | + status_code=status, |
| 23 | + request=request, |
| 24 | + headers={"content-type": "application/json"}, |
| 25 | + content=content, |
| 26 | + ) |
| 27 | + http_err = httpx.HTTPStatusError("error", request=request, response=response) |
| 28 | + enriched = EnrichedException(http_err) |
| 29 | + enriched.__cause__ = http_err |
| 30 | + return enriched |
| 31 | + |
| 32 | + |
| 33 | +_KNOWN_ERRORS: dict[tuple[int, str | None], tuple[str, UiPathErrorCategory]] = { |
| 34 | + (404, "1002"): ( |
| 35 | + "Could not find process for tool '{tool}'.", |
| 36 | + UiPathErrorCategory.USER, |
| 37 | + ), |
| 38 | + (400, "1100"): ( |
| 39 | + "Folder not found for tool '{tool}'.", |
| 40 | + UiPathErrorCategory.USER, |
| 41 | + ), |
| 42 | + (409, None): ( |
| 43 | + "Cannot start tool '{tool}': {message}", |
| 44 | + UiPathErrorCategory.DEPLOYMENT, |
| 45 | + ), |
| 46 | +} |
| 47 | + |
| 48 | +_TITLE = "Failed to execute tool 'MyProcess'" |
| 49 | + |
| 50 | + |
| 51 | +class TestMatching: |
| 52 | + def test_exact_match(self) -> None: |
| 53 | + err = _make_enriched(404, {"errorCode": "1002", "message": "Not found"}) |
| 54 | + with pytest.raises(AgentRuntimeError) as exc_info: |
| 55 | + raise_for_enriched(err, _KNOWN_ERRORS, title=_TITLE, tool="MyProcess") |
| 56 | + assert exc_info.value.error_info.category == UiPathErrorCategory.USER |
| 57 | + assert "MyProcess" in exc_info.value.error_info.detail |
| 58 | + |
| 59 | + def test_wildcard_match(self) -> None: |
| 60 | + err = _make_enriched(409, {"message": "Already running"}) |
| 61 | + with pytest.raises(AgentRuntimeError) as exc_info: |
| 62 | + raise_for_enriched(err, _KNOWN_ERRORS, title=_TITLE, tool="MyTool") |
| 63 | + assert "Already running" in exc_info.value.error_info.detail |
| 64 | + assert "MyTool" in exc_info.value.error_info.detail |
| 65 | + |
| 66 | + def test_specific_beats_wildcard(self) -> None: |
| 67 | + errors: dict[tuple[int, str | None], tuple[str, UiPathErrorCategory]] = { |
| 68 | + (404, "1002"): ("specific: {tool}", UiPathErrorCategory.DEPLOYMENT), |
| 69 | + (404, None): ("wildcard: {tool}", UiPathErrorCategory.SYSTEM), |
| 70 | + } |
| 71 | + err = _make_enriched(404, {"errorCode": "1002"}) |
| 72 | + with pytest.raises(AgentRuntimeError) as exc_info: |
| 73 | + raise_for_enriched(err, errors, title=_TITLE, tool="T") |
| 74 | + assert exc_info.value.error_info.detail == "specific: T" |
| 75 | + assert exc_info.value.error_info.category == UiPathErrorCategory.DEPLOYMENT |
| 76 | + |
| 77 | + def test_no_match_does_nothing(self) -> None: |
| 78 | + err = _make_enriched(500, {"message": "Server error"}) |
| 79 | + raise_for_enriched(err, _KNOWN_ERRORS, title=_TITLE, tool="T") |
| 80 | + |
| 81 | + def test_unknown_error_code_does_nothing(self) -> None: |
| 82 | + err = _make_enriched(404, {"errorCode": "9999"}) |
| 83 | + raise_for_enriched(err, _KNOWN_ERRORS, title=_TITLE, tool="T") |
| 84 | + |
| 85 | + |
| 86 | +class TestTitleAndDetail: |
| 87 | + def test_title_is_fixed(self) -> None: |
| 88 | + err = _make_enriched(404, {"errorCode": "1002", "message": "Not found"}) |
| 89 | + with pytest.raises(AgentRuntimeError) as exc_info: |
| 90 | + raise_for_enriched(err, _KNOWN_ERRORS, title=_TITLE, tool="T") |
| 91 | + assert exc_info.value.error_info.title == _TITLE |
| 92 | + |
| 93 | + def test_detail_uses_template(self) -> None: |
| 94 | + err = _make_enriched(404, {"errorCode": "1002", "message": "Not found"}) |
| 95 | + with pytest.raises(AgentRuntimeError) as exc_info: |
| 96 | + raise_for_enriched(err, _KNOWN_ERRORS, title=_TITLE, tool="InvoiceBot") |
| 97 | + assert ( |
| 98 | + exc_info.value.error_info.detail |
| 99 | + == "Could not find process for tool 'InvoiceBot'." |
| 100 | + ) |
| 101 | + |
| 102 | + def test_message_placeholder(self) -> None: |
| 103 | + err = _make_enriched(409, {"message": "Job conflict"}) |
| 104 | + with pytest.raises(AgentRuntimeError) as exc_info: |
| 105 | + raise_for_enriched(err, _KNOWN_ERRORS, title=_TITLE, tool="T") |
| 106 | + assert "Job conflict" in exc_info.value.error_info.detail |
| 107 | + |
| 108 | + def test_empty_message_when_no_error_info(self) -> None: |
| 109 | + err = _make_enriched(409, body=None) |
| 110 | + with pytest.raises(AgentRuntimeError) as exc_info: |
| 111 | + raise_for_enriched(err, _KNOWN_ERRORS, title=_TITLE, tool="T") |
| 112 | + assert "Cannot start tool 'T': " in exc_info.value.error_info.detail |
| 113 | + |
| 114 | + def test_missing_context_renders_as_unknown(self) -> None: |
| 115 | + err = _make_enriched(404, {"errorCode": "1002"}) |
| 116 | + with pytest.raises(AgentRuntimeError) as exc_info: |
| 117 | + raise_for_enriched(err, _KNOWN_ERRORS, title=_TITLE) |
| 118 | + assert "<unknown>" in exc_info.value.error_info.detail |
| 119 | + |
| 120 | + |
| 121 | +class TestErrorProperties: |
| 122 | + def test_error_code(self) -> None: |
| 123 | + err = _make_enriched(404, {"errorCode": "1002"}) |
| 124 | + with pytest.raises(AgentRuntimeError) as exc_info: |
| 125 | + raise_for_enriched(err, _KNOWN_ERRORS, title=_TITLE, tool="T") |
| 126 | + assert exc_info.value.error_info.code == AgentRuntimeError.full_code( |
| 127 | + AgentRuntimeErrorCode.HTTP_ERROR |
| 128 | + ) |
| 129 | + |
| 130 | + def test_status_code_preserved(self) -> None: |
| 131 | + err = _make_enriched(400, {"errorCode": "1100"}) |
| 132 | + with pytest.raises(AgentRuntimeError) as exc_info: |
| 133 | + raise_for_enriched(err, _KNOWN_ERRORS, title=_TITLE, tool="T") |
| 134 | + assert exc_info.value.error_info.status == 400 |
| 135 | + |
| 136 | + def test_original_exception_chained(self) -> None: |
| 137 | + err = _make_enriched(404, {"errorCode": "1002"}) |
| 138 | + with pytest.raises(AgentRuntimeError) as exc_info: |
| 139 | + raise_for_enriched(err, _KNOWN_ERRORS, title=_TITLE, tool="T") |
| 140 | + assert exc_info.value.__cause__ is err |
0 commit comments