From a6df17d4db460f47688f8ab7fb85c55a98f02f41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20M=C3=BCller?= Date: Mon, 15 Jul 2024 16:37:42 +0200 Subject: [PATCH] make parsing even nicer --- pyproject.toml | 2 +- tabpfn_client/client.py | 45 +++++++++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 22112b1..a151613 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "tabpfn-client" -version = "0.0.18" +version = "0.0.19" requires-python = ">=3.10" dependencies = [ "httpx>=0.24.1", diff --git a/tabpfn_client/client.py b/tabpfn_client/client.py index d6a6a63..b7fc563 100644 --- a/tabpfn_client/client.py +++ b/tabpfn_client/client.py @@ -160,7 +160,9 @@ def predict( return result @staticmethod - def _validate_response(response, method_name, only_version_check=False): + def _validate_response( + response: httpx.Response, method_name, only_version_check=False + ): # If status code is 200, no errors occurred on the server side. if response.status_code == 200: return @@ -186,22 +188,35 @@ def _validate_response(response, method_name, only_version_check=False): logger.error( f"Fail to call {method_name}, response status: {response.status_code}" ) - if ( - len( - reponse_split_up := response.text.split( - "The following exception has occurred:" + try: + if ( + len( + reponse_split_up := response.text.split( + "The following exception has occurred:" + ) ) + > 1 + ): + relevant_reponse_text = reponse_split_up[1].split( + "debug_error_string" + )[0] + if "ValueError" in relevant_reponse_text: + # Extract the ValueError message + value_error_msg = relevant_reponse_text.split( + "ValueError. Arguments: (" + )[1].split(",)")[0] + # Remove extra quotes and spaces + value_error_msg = value_error_msg.strip("'") + # Raise the ValueError with the extracted message + raise ValueError(value_error_msg) + raise RuntimeError(relevant_reponse_text) + except Exception as e: + if isinstance(e, (ValueError, RuntimeError)): + raise e + raise RuntimeError( + f"Fail to call {method_name} with error: {response.status_code}, reason: " + f"{response.reason_phrase} and text: {response.text}" ) - > 1 - ): - relevant_reponse_test = reponse_split_up[1].split("debug_error_string")[ - 0 - ] - raise RuntimeError(relevant_reponse_test) - raise RuntimeError( - f"Fail to call {method_name} with error: {response.status_code} and reason: " - f"{response.reason_phrase}" - ) def try_connection(self) -> bool: """