diff --git a/src/unstract/sdk/adapter.py b/src/unstract/sdk/adapter.py index 75bb6c2b..538866a3 100644 --- a/src/unstract/sdk/adapter.py +++ b/src/unstract/sdk/adapter.py @@ -2,8 +2,11 @@ from typing import Any, Optional import requests +from requests.exceptions import ConnectionError, HTTPError -from unstract.sdk.constants import AdapterKeys, LogLevel, ToolEnv +from unstract.sdk.adapters.utils import AdapterUtils +from unstract.sdk.constants import AdapterKeys, ToolEnv +from unstract.sdk.exceptions import SdkError from unstract.sdk.helper import SdkHelper from unstract.sdk.platform import PlatformBase from unstract.sdk.tool.base import BaseTool @@ -37,52 +40,46 @@ def __init__( tool=tool, platform_host=platform_host, platform_port=platform_port ) - def get_adapter_configuration( + def _get_adapter_configuration( self, adapter_instance_id: str, - ) -> Optional[dict[str, Any]]: + ) -> dict[str, Any]: """Get Adapter 1. Get the adapter config from platform service using the adapter_instance_id Args: - adapter_instance_id (str): adapter Instance Id + adapter_instance_id (str): Adapter instance ID Returns: - Any: _description_ + dict[str, Any]: Config stored for the adapter """ url = f"{self.base_url}/adapter_instance" query_params = {AdapterKeys.ADAPTER_INSTANCE_ID: adapter_instance_id} headers = {"Authorization": f"Bearer {self.bearer_token}"} - response = requests.get(url, headers=headers, params=query_params) - if response.status_code == 200: + try: + response = requests.get(url, headers=headers, params=query_params) + response.raise_for_status() adapter_data: dict[str, Any] = response.json() # TODO: Print config after redacting sensitive information self.tool.stream_log( - "Successfully retrieved adapter config " - f"for adapter: {adapter_instance_id}" + "Successfully retrieved config " + f"for adapter instance {adapter_instance_id}" ) - - return adapter_data - - elif response.status_code == 404: - self.tool.stream_log( - f"adapter not found for: for adapter instance" f"{adapter_instance_id}", - level=LogLevel.ERROR, + except ConnectionError: + raise SdkError( + "Unable to connect to platform service, please contact the admin." ) - return None - - else: - self.tool.stream_log( - ( - f"Error while retrieving adapter " - "for adapter instance: " - f"{adapter_instance_id} / {response.reason}" - ), - level=LogLevel.ERROR, + except HTTPError as e: + default_err = ( + "Error while calling the platform service, please contact the admin." + ) + msg = AdapterUtils.get_msg_from_request_exc( + err=e, message_key="error", default_err=default_err ) - return None + raise SdkError(f"Error while retrieving adapter. {msg}") + return adapter_data @staticmethod def get_adapter_config( @@ -96,13 +93,13 @@ def get_adapter_config( platform service to retrieve the configuration. Args: - adapter_instance_id (str): ID of the adapter instance tool (AbstractTool): Instance of AbstractTool + adapter_instance_id (str): ID of the adapter instance Required env variables: PLATFORM_HOST: Host of platform service PLATFORM_PORT: Port of platform service Returns: - Any: engine + dict[str, Any]: Config stored for the adapter """ # Check if the adapter ID matches any public adapter keys if SdkHelper.is_public_adapter(adapter_id=adapter_instance_id): @@ -120,11 +117,4 @@ def get_adapter_config( platform_host=platform_host, platform_port=platform_port, ) - adapter_metadata: Optional[ - dict[str, Any] - ] = tool_adapter.get_adapter_configuration(adapter_instance_id) - if not adapter_metadata: - tool.stream_error_and_exit( - f"Adapter not found for " f"adapter instance: {adapter_instance_id}" - ) - return adapter_metadata + return tool_adapter._get_adapter_configuration(adapter_instance_id) diff --git a/src/unstract/sdk/adapters/utils.py b/src/unstract/sdk/adapters/utils.py index 6891212a..bfc6b1ae 100644 --- a/src/unstract/sdk/adapters/utils.py +++ b/src/unstract/sdk/adapters/utils.py @@ -31,7 +31,7 @@ def get_msg_from_request_exc( if message_key in err_json: return str(err_json[message_key]) elif err_response.headers["Content-Type"] == "text/plain": - return err.response.text # type: ignore + return err_response.text # type: ignore return default_err @staticmethod diff --git a/src/unstract/sdk/tool/executor.py b/src/unstract/sdk/tool/executor.py index a40ebc5c..603a4ab5 100644 --- a/src/unstract/sdk/tool/executor.py +++ b/src/unstract/sdk/tool/executor.py @@ -49,8 +49,9 @@ def execute_run(self, args: argparse.Namespace) -> None: self.tool.stream_error_and_exit("--settings are required for RUN command") settings: dict[str, Any] = loads(args.settings) + tool_name = self.tool.properties["displayName"] self.tool.stream_log( - f"Running tool with " + f"Running tool '{tool_name}' with " f"Workflow ID: {self.tool.workflow_id}, " f"Execution ID: {self.tool.execution_id}, " f"SDK Version: {get_sdk_version()}" @@ -72,7 +73,8 @@ def execute_run(self, args: argparse.Namespace) -> None: output_dir=self.tool.get_output_dir(), ) except Exception as e: - logger.error(f"Error while tool run: {e}", stack_info=True, exc_info=True) - self.tool.stream_error_and_exit(f"Error while running tool: {str(e)}") + msg = f"Error while running tool '{tool_name}': {str(e)}" + logger.error(msg, stack_info=True, exc_info=True) + self.tool.stream_error_and_exit(msg) # TODO: Call tool method to validate if output was written diff --git a/src/unstract/sdk/tool/stream.py b/src/unstract/sdk/tool/stream.py index ca300553..76c91b73 100644 --- a/src/unstract/sdk/tool/stream.py +++ b/src/unstract/sdk/tool/stream.py @@ -94,7 +94,7 @@ def get_env_or_die(self, env_key: str) -> str: """ env_value = os.environ.get(env_key) if env_value is None or env_value == "": - self.stream_error_and_exit(f"Env variable {env_key} is required") + self.stream_error_and_exit(f"Env variable '{env_key}' is required") return env_value @staticmethod