From 138d4194c33e4b64b4bc98c0cce7a2cbfcd1edf1 Mon Sep 17 00:00:00 2001 From: Anton Dubovik Date: Thu, 9 Jan 2025 14:31:41 +0100 Subject: [PATCH] fix: define str of HTTPException via its message (#210) --- aidial_sdk/exceptions.py | 1 + tests/test_exception.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 tests/test_exception.py diff --git a/aidial_sdk/exceptions.py b/aidial_sdk/exceptions.py index e844c91..75103e0 100644 --- a/aidial_sdk/exceptions.py +++ b/aidial_sdk/exceptions.py @@ -20,6 +20,7 @@ def __init__( display_message: Optional[str] = None, headers: Optional[Dict[str, str]] = None, ) -> None: + super().__init__(message) status_code = int(status_code) self.message = message diff --git a/tests/test_exception.py b/tests/test_exception.py new file mode 100644 index 0000000..1f2d12a --- /dev/null +++ b/tests/test_exception.py @@ -0,0 +1,31 @@ +from typing import List, Tuple + +import pytest + +from aidial_sdk.exceptions import HTTPException, TruncatePromptSystemError + +test_cases: List[Tuple[HTTPException, str]] = [ + ( + HTTPException( + message="message", + status_code=400, + type="type", + param="param", + code="code", + display_message="display_message", + headers={"header": "value"}, + ), + "message", + ), + ( + TruncatePromptSystemError(1, 20), + "The requested maximum prompt tokens is 1. " + "However, the system messages resulted in 20 tokens. " + "Please reduce the length of the system messages or increase the maximum prompt tokens.", + ), +] + + +@pytest.mark.parametrize("exc, expected", test_cases) +def test_str_exception(exc: HTTPException, expected: str): + assert str(exc) == expected