Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(client): preserve structured server error details (#5559)

Zhenghan Song62ac99f54e10
diff --git a/openviking_cli/client/_http_compat.py b/openviking_cli/client/_http_compat.py
index d49d9fc25..2ef6165e6 100644
--- a/openviking_cli/client/_http_compat.py
+++ b/openviking_cli/client/_http_compat.py
@@ -86,28 +86,32 @@ def _raise_legacy_exception(error: Dict[str, Any]) -> None:
exc_class = ERROR_CODE_TO_EXCEPTION.get(code, OpenVikingError)
if exc_class == OpenVikingError:
- raise exc_class(message, code=code, details=details)
- if exc_class in (
+ exception = exc_class(message, code=code, details=details)
+ elif exc_class in (
InvalidArgumentError,
FailedPreconditionError,
ResourceExhaustedError,
AbortedError,
UnimplementedError,
):
- raise exc_class(message, details=details)
- if exc_class == InvalidURIError:
+ exception = exc_class(message, details=details)
+ elif exc_class == InvalidURIError:
uri = details.get("uri", "") if details else ""
reason = details.get("reason", "") if details else ""
- raise exc_class(uri, reason)
- if exc_class == NotFoundError:
+ exception = exc_class(uri, reason)
+ elif exc_class == NotFoundError:
resource = details.get("resource", "") if details else ""
resource_type = details.get("type", "resource") if details else "resource"
- raise exc_class(resource, resource_type)
- if exc_class == AlreadyExistsError:
+ exception = exc_class(resource, resource_type)
+ elif exc_class == AlreadyExistsError:
resource = details.get("resource", "") if details else ""
resource_type = details.get("type", "resource") if details else "resource"
- raise exc_class(resource, resource_type)
- raise exc_class(message)
+ exception = exc_class(resource, resource_type)
+ else:
+ exception = exc_class(message)
+ if details is not None:
+ exception.details.update(details)
+ raise exception
class AsyncHTTPClient(import_openviking_sdk().AsyncHTTPClient):
diff --git a/sdk/python/openviking_sdk/client.py b/sdk/python/openviking_sdk/client.py
index 0c4822f1a..8debc7bfb 100644
--- a/sdk/python/openviking_sdk/client.py
+++ b/sdk/python/openviking_sdk/client.py
@@ -653,33 +653,37 @@ class AsyncHTTPClient:
exc_class = ERROR_CODE_TO_EXCEPTION.get(code, OpenVikingError)
if exc_class == OpenVikingError:
- raise exc_class(message, code=code, details=details)
- if exc_class in (
+ exception = exc_class(message, code=code, details=details)
+ elif exc_class in (
InvalidArgumentError,
FailedPreconditionError,
ResourceExhaustedError,
AbortedError,
UnimplementedError,
):
- raise exc_class(message, details=details)
- if exc_class == InvalidURIError:
+ exception = exc_class(message, details=details)
+ elif exc_class == InvalidURIError:
uri = details.get("uri", "") if details else ""
reason = details.get("reason", "") if details else ""
- raise exc_class(uri, reason)
- if exc_class == NotFoundError:
+ exception = exc_class(uri, reason)
+ elif exc_class == NotFoundError:
resource = details.get("resource", "") if details else ""
resource_type = details.get("type", "resource") if details else "resource"
reason = details.get("reason") if details else None
- raise exc_class(resource, resource_type, reason=reason)
- if exc_class == AlreadyExistsError:
+ exception = exc_class(resource, resource_type, reason=reason)
+ elif exc_class == AlreadyExistsError:
resource = details.get("resource", "") if details else ""
resource_type = details.get("type", "resource") if details else "resource"
- raise exc_class(resource, resource_type)
- if exc_class == UnavailableError:
+ exception = exc_class(resource, resource_type)
+ elif exc_class == UnavailableError:
service = details.get("service", "service") if details else "service"
reason = details.get("reason", "") if details else message
- raise exc_class(service, reason)
- raise exc_class(message)
+ exception = exc_class(service, reason)
+ else:
+ exception = exc_class(message)
+ if details is not None:
+ exception.details.update(details)
+ raise exception
def _zip_directory(self, dir_path: str) -> str:
return zip_directory(dir_path)
diff --git a/sdk/python/tests/test_error_mapping.py b/sdk/python/tests/test_error_mapping.py
index 5fd218da9..d6a1f9460 100644
--- a/sdk/python/tests/test_error_mapping.py
+++ b/sdk/python/tests/test_error_mapping.py
@@ -1,8 +1,10 @@
# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
# SPDX-License-Identifier: AGPL-3.0
+import httpx
import pytest
-from openviking_sdk import AsyncHTTPClient
+from openviking_sdk import AsyncHTTPClient, SyncHTTPClient
+from openviking_sdk.client import ERROR_CODE_TO_EXCEPTION
from openviking_sdk.errors import (
AbortedError,
ConflictError,
@@ -13,6 +15,43 @@ from openviking_sdk.errors import (
)
[email protected]("sync", [False, True])
[email protected]("code", ERROR_CODE_TO_EXCEPTION)
+async def test_public_clients_preserve_server_error_details(sync, code):
+ details = {
+ "resource": "viking://resources/demo",
+ "uri": "viking://resources/demo",
+ "retryable": True,
+ "upstream_status_code": 503,
+ "provider": {"retry_after": 2},
+ }
+ client = (SyncHTTPClient if sync else AsyncHTTPClient)(url="http://localhost:1933")
+ async_client = client._async_client if sync else client
+ async_client._http = httpx.AsyncClient(
+ base_url="http://localhost:1933",
+ transport=httpx.MockTransport(
+ lambda _request: httpx.Response(
+ 409,
+ json={
+ "status": "error",
+ "error": {"code": code, "message": "busy", "details": details},
+ },
+ )
+ ),
+ )
+ try:
+ with pytest.raises(OpenVikingError) as raised:
+ if sync:
+ client.stat("viking://resources/demo")
+ else:
+ await client.stat("viking://resources/demo")
+ assert raised.value.code == code
+ assert {key: raised.value.details[key] for key in details} == details
+ finally:
+ await async_client.close()
+
+
@pytest.mark.parametrize(
("code", "exc_type"),
(
diff --git a/sdk/python/tests/test_legacy_shims.py b/sdk/python/tests/test_legacy_shims.py
index 2089f02b2..5846bad6c 100644
--- a/sdk/python/tests/test_legacy_shims.py
+++ b/sdk/python/tests/test_legacy_shims.py
@@ -4,7 +4,9 @@
import sys
from pathlib import Path
+import httpx
import pytest
+from openviking_sdk.client import ERROR_CODE_TO_EXCEPTION
SDK_ROOT = Path(__file__).resolve().parents[1]
REPO_ROOT = Path(__file__).resolve().parents[3]
@@ -71,3 +73,44 @@ def test_legacy_sync_http_client_shim_points_to_sdk():
client = LegacySyncHTTPClient(url="http://localhost:1933")
assert client._async_client._url == "http://localhost:1933"
+
+
[email protected]("sync", [False, True])
[email protected]("code", ERROR_CODE_TO_EXCEPTION)
+async def test_legacy_clients_preserve_server_error_details(sync, code):
+ from openviking_cli.client.http import AsyncHTTPClient
+ from openviking_cli.client.sync_http import SyncHTTPClient
+ from openviking_cli.exceptions import OpenVikingError
+
+ details = {
+ "resource": "viking://resources/demo",
+ "uri": "viking://resources/demo",
+ "retryable": True,
+ "upstream_status_code": 503,
+ "provider": {"retry_after": 2},
+ }
+ client = (SyncHTTPClient if sync else AsyncHTTPClient)(url="http://localhost:1933")
+ async_client = client._async_client if sync else client
+ async_client._http = httpx.AsyncClient(
+ base_url="http://localhost:1933",
+ transport=httpx.MockTransport(
+ lambda _request: httpx.Response(
+ 409,
+ json={
+ "status": "error",
+ "error": {"code": code, "message": "busy", "details": details},
+ },
+ )
+ ),
+ )
+ try:
+ with pytest.raises(OpenVikingError) as raised:
+ if sync:
+ client.stat("viking://resources/demo")
+ else:
+ await client.stat("viking://resources/demo")
+ assert raised.value.code == code
+ assert {key: raised.value.details[key] for key in details} == details
+ finally:
+ await async_client.close()