|
| 1 | +"""Tests for normalizing provider HTTP errors into a common shape. |
| 2 | +
|
| 3 | +Each provider behind the LLM Gateway raises a different exception type, but all |
| 4 | +carry the same gateway body (``{status, detail, ...}``). ``extract_provider_error`` |
| 5 | +reads the status code + detail off whichever attribute the SDK exposes, walking |
| 6 | +the ``__cause__`` chain when LangChain wraps the SDK error. |
| 7 | +""" |
| 8 | + |
| 9 | +import pytest |
| 10 | +from uipath.runtime.errors import UiPathErrorCategory |
| 11 | + |
| 12 | +from uipath_langchain.agent.exceptions.exceptions import ( |
| 13 | + AgentRuntimeError, |
| 14 | + AgentRuntimeErrorCode, |
| 15 | +) |
| 16 | +from uipath_langchain.agent.exceptions.licensing import raise_for_provider_http_error |
| 17 | +from uipath_langchain.chat.provider_errors import ( |
| 18 | + ProviderError, |
| 19 | + extract_provider_error, |
| 20 | +) |
| 21 | + |
| 22 | +_DETAIL = "License not available for LLM usage. You need additional 'AGU'." |
| 23 | +_BODY = { |
| 24 | + "title": "License not available", |
| 25 | + "status": 403, |
| 26 | + "detail": _DETAIL, |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | +class TestExtractProviderError: |
| 31 | + def test_openai_status_code_and_body(self) -> None: |
| 32 | + class OpenAIError(Exception): |
| 33 | + status_code = 403 |
| 34 | + body = _BODY |
| 35 | + |
| 36 | + result = extract_provider_error(OpenAIError("Forbidden")) |
| 37 | + assert result == ProviderError(status_code=403, detail=_DETAIL) |
| 38 | + |
| 39 | + def test_bedrock_uipath_api_error_same_shape_as_openai(self) -> None: |
| 40 | + # Bedrock via WrappedBotoClient surfaces as a UiPathAPIError (httpx |
| 41 | + # subclass) exposing OpenAI-style .status_code / .body. |
| 42 | + class UiPathPermissionDeniedError(Exception): |
| 43 | + status_code = 403 |
| 44 | + body = _BODY |
| 45 | + |
| 46 | + result = extract_provider_error(UiPathPermissionDeniedError("Forbidden")) |
| 47 | + assert result == ProviderError(status_code=403, detail=_DETAIL) |
| 48 | + |
| 49 | + def test_vertex_wrapped_in_langchain_error(self) -> None: |
| 50 | + # google.genai exposes .code + .details; LangChain wraps it in a class |
| 51 | + # that itself exposes nothing, so the fields live on the __cause__. |
| 52 | + class GenAIError(Exception): |
| 53 | + code = 403 |
| 54 | + details = _BODY |
| 55 | + |
| 56 | + class ChatGoogleGenerativeAIError(Exception): |
| 57 | + pass |
| 58 | + |
| 59 | + try: |
| 60 | + try: |
| 61 | + raise GenAIError("403") |
| 62 | + except GenAIError as cause: |
| 63 | + raise ChatGoogleGenerativeAIError("wrapped") from cause |
| 64 | + except ChatGoogleGenerativeAIError as wrapper: |
| 65 | + result = extract_provider_error(wrapper) |
| 66 | + |
| 67 | + assert result == ProviderError(status_code=403, detail=_DETAIL) |
| 68 | + |
| 69 | + def test_botocore_response_dict(self) -> None: |
| 70 | + # Legacy direct boto3 path: botocore.ClientError carries a response dict. |
| 71 | + class ClientError(Exception): |
| 72 | + response = { |
| 73 | + "ResponseMetadata": {"HTTPStatusCode": 403}, |
| 74 | + "Error": {"Code": "AccessDenied", "detail": _BODY["detail"]}, |
| 75 | + } |
| 76 | + |
| 77 | + result = extract_provider_error(ClientError("denied")) |
| 78 | + assert result.status_code == 403 |
| 79 | + |
| 80 | + def test_none_returns_empty(self) -> None: |
| 81 | + result = extract_provider_error(None) |
| 82 | + assert result == ProviderError() |
| 83 | + assert not result |
| 84 | + |
| 85 | + def test_non_int_status_attribute_is_ignored(self) -> None: |
| 86 | + # An unrelated exception that happens to carry a string `code` must not |
| 87 | + # be mistaken for a provider HTTP error. |
| 88 | + class OSLike(Exception): |
| 89 | + code = "ENOENT" |
| 90 | + |
| 91 | + assert extract_provider_error(OSLike("nope")) == ProviderError() |
| 92 | + |
| 93 | + |
| 94 | +class TestRaiseForProviderHttpError: |
| 95 | + def test_403_maps_to_license_not_available(self) -> None: |
| 96 | + class OpenAIError(Exception): |
| 97 | + status_code = 403 |
| 98 | + body = _BODY |
| 99 | + |
| 100 | + with pytest.raises(AgentRuntimeError) as exc_info: |
| 101 | + raise_for_provider_http_error(OpenAIError("Forbidden")) |
| 102 | + |
| 103 | + info = exc_info.value.error_info |
| 104 | + assert info.status == 403 |
| 105 | + assert info.category == UiPathErrorCategory.DEPLOYMENT |
| 106 | + assert info.code.endswith(AgentRuntimeErrorCode.LICENSE_NOT_AVAILABLE.value) |
| 107 | + assert _DETAIL in info.detail |
| 108 | + |
| 109 | + def test_other_status_falls_back_to_http_error_and_str(self) -> None: |
| 110 | + # Non-403 status, and no `detail` in the body → detail falls back to str(e). |
| 111 | + class OpenAIError(Exception): |
| 112 | + status_code = 500 |
| 113 | + body: dict[str, str] = {} |
| 114 | + |
| 115 | + with pytest.raises(AgentRuntimeError) as exc_info: |
| 116 | + raise_for_provider_http_error(OpenAIError("boom")) |
| 117 | + |
| 118 | + info = exc_info.value.error_info |
| 119 | + assert info.status == 500 |
| 120 | + assert info.category == UiPathErrorCategory.UNKNOWN |
| 121 | + assert info.code.endswith(AgentRuntimeErrorCode.HTTP_ERROR.value) |
| 122 | + assert "boom" in info.detail # str(e) fallback |
| 123 | + |
| 124 | + def test_no_status_does_not_raise(self) -> None: |
| 125 | + # No extractable HTTP status → no-op (the original exception is left to |
| 126 | + # propagate from the caller). Reaching the end without raising is the assert. |
| 127 | + raise_for_provider_http_error(ValueError("unrelated transport error")) |
0 commit comments