diff --git a/src/conductor/client/exceptions/api_error.py b/src/conductor/client/exceptions/api_error.py index b7e2644c..856e4bbc 100644 --- a/src/conductor/client/exceptions/api_error.py +++ b/src/conductor/client/exceptions/api_error.py @@ -2,20 +2,18 @@ class APIErrorCode(str, Enum): - NOT_FOUND = "NOT_FOUND", - FORBIDDEN = "FORBIDDEN" - CONFLICT = "CONFLICT" - BAD_REQUEST = "BAD_REQUEST" - REQUEST_TIMEOUT = "REQUEST_TIMEOUT" - UNKNOWN = "UNKNOWN" + NOT_FOUND = 404, + FORBIDDEN = 403 + CONFLICT = 409 + BAD_REQUEST = 400 + REQUEST_TIMEOUT = 408 + UNKNOWN = 0 -class APIError(BaseException): - def __init__(self, code, message): - self.code = code - self.message = message +class APIError(Exception): - super().__init__(message) + def __init__(self, status=None, reason=None, http_resp=None, body=None): + super().__init__(status, reason, http_resp, body) def __str__(self): return "APIError: code={} message={}".format(self.code, self.message) diff --git a/src/conductor/client/exceptions/api_exception_handler.py b/src/conductor/client/exceptions/api_exception_handler.py index 807642ba..ef9528bc 100644 --- a/src/conductor/client/exceptions/api_exception_handler.py +++ b/src/conductor/client/exceptions/api_exception_handler.py @@ -17,7 +17,6 @@ def inner_function(*args, **kwargs): try: return function(*args, **kwargs) except ApiException as e: - message = "Unknown failure" if e.status == 404: code = APIErrorCode.NOT_FOUND diff --git a/src/conductor/client/http/rest.py b/src/conductor/client/http/rest.py index fcf57a3c..7f0bd6ef 100644 --- a/src/conductor/client/http/rest.py +++ b/src/conductor/client/http/rest.py @@ -185,13 +185,24 @@ class ApiException(Exception): def __init__(self, status=None, reason=None, http_resp=None, body=None): if http_resp: self.status = http_resp.status + self.code = http_resp.status self.reason = http_resp.reason self.body = http_resp.resp.text + try: + if http_resp.resp.text: + error = json.loads(http_resp.resp.text) + self.message = error['message'] + else: + self.message = http_resp.resp.text + except Exception as e: + self.message = http_resp.resp.text self.headers = http_resp.getheaders() else: self.status = status + self.code = status self.reason = reason self.body = body + self.message = body self.headers = None def __str__(self): diff --git a/src/conductor/client/orkes/orkes_authorization_client.py b/src/conductor/client/orkes/orkes_authorization_client.py index 176cdd95..fce6715e 100644 --- a/src/conductor/client/orkes/orkes_authorization_client.py +++ b/src/conductor/client/orkes/orkes_authorization_client.py @@ -2,7 +2,6 @@ from conductor.client.authorization_client import AuthorizationClient from conductor.client.configuration.configuration import Configuration -from conductor.client.exceptions.api_exception_handler import api_exception_handler, for_all_methods from conductor.client.http.models.authorization_request import AuthorizationRequest from conductor.client.http.models.conductor_application import ConductorApplication from conductor.client.http.models.conductor_user import ConductorUser @@ -20,7 +19,6 @@ from conductor.client.orkes.orkes_base_client import OrkesBaseClient -@for_all_methods(api_exception_handler, ["__init__"]) class OrkesAuthorizationClient(OrkesBaseClient, AuthorizationClient): def __init__(self, configuration: Configuration): super(OrkesAuthorizationClient, self).__init__(configuration) diff --git a/src/conductor/client/orkes/orkes_integration_client.py b/src/conductor/client/orkes/orkes_integration_client.py index 607a726f..498b07c9 100644 --- a/src/conductor/client/orkes/orkes_integration_client.py +++ b/src/conductor/client/orkes/orkes_integration_client.py @@ -3,7 +3,6 @@ from typing import List from conductor.client.configuration.configuration import Configuration -from conductor.client.exceptions.api_exception_handler import api_exception_handler, for_all_methods from conductor.client.http.models.integration import Integration from conductor.client.http.models.integration_api import IntegrationApi from conductor.client.http.models.integration_api_update import IntegrationApiUpdate @@ -13,7 +12,6 @@ from conductor.client.orkes.orkes_base_client import OrkesBaseClient -@for_all_methods(api_exception_handler, ["__init__"]) class OrkesIntegrationClient(OrkesBaseClient, IntegrationClient): def __init__(self, configuration: Configuration): diff --git a/src/conductor/client/orkes/orkes_metadata_client.py b/src/conductor/client/orkes/orkes_metadata_client.py index dc69e6f9..1d7ceaf1 100644 --- a/src/conductor/client/orkes/orkes_metadata_client.py +++ b/src/conductor/client/orkes/orkes_metadata_client.py @@ -1,7 +1,6 @@ from typing import Optional, List from conductor.client.configuration.configuration import Configuration -from conductor.client.exceptions.api_exception_handler import api_exception_handler, for_all_methods from conductor.client.http.models.tag_string import TagString from conductor.client.http.models.task_def import TaskDef from conductor.client.http.models.workflow_def import WorkflowDef @@ -11,7 +10,6 @@ from conductor.client.orkes.orkes_base_client import OrkesBaseClient -@for_all_methods(api_exception_handler, ["__init__"]) class OrkesMetadataClient(OrkesBaseClient, MetadataClient): def __init__(self, configuration: Configuration): super(OrkesMetadataClient, self).__init__(configuration) diff --git a/src/conductor/client/orkes/orkes_prompt_client.py b/src/conductor/client/orkes/orkes_prompt_client.py index 2ffd36c9..a3158bf5 100644 --- a/src/conductor/client/orkes/orkes_prompt_client.py +++ b/src/conductor/client/orkes/orkes_prompt_client.py @@ -4,7 +4,6 @@ from typing import List from conductor.client.configuration.configuration import Configuration -from conductor.client.exceptions.api_exception_handler import api_exception_handler, for_all_methods from conductor.client.http.models.prompt_template import PromptTemplate from conductor.client.http.models.prompt_test_request import PromptTemplateTestRequest from conductor.client.orkes.models.metadata_tag import MetadataTag @@ -15,7 +14,6 @@ # python 2 and python 3 compatibility library -@for_all_methods(api_exception_handler, ["__init__"]) class OrkesPromptClient(OrkesBaseClient, PromptClient): def __init__(self, configuration: Configuration): diff --git a/src/conductor/client/orkes/orkes_scheduler_client.py b/src/conductor/client/orkes/orkes_scheduler_client.py index 7e5fc3bb..bfbe8282 100644 --- a/src/conductor/client/orkes/orkes_scheduler_client.py +++ b/src/conductor/client/orkes/orkes_scheduler_client.py @@ -1,7 +1,6 @@ from typing import Optional, List from conductor.client.configuration.configuration import Configuration -from conductor.client.exceptions.api_exception_handler import api_exception_handler, for_all_methods from conductor.client.http.models.save_schedule_request import SaveScheduleRequest from conductor.client.http.models.search_result_workflow_schedule_execution_model import \ SearchResultWorkflowScheduleExecutionModel @@ -11,7 +10,6 @@ from conductor.client.scheduler_client import SchedulerClient -@for_all_methods(api_exception_handler, ["__init__"]) class OrkesSchedulerClient(OrkesBaseClient, SchedulerClient): def __init__(self, configuration: Configuration): super(OrkesSchedulerClient, self).__init__(configuration) diff --git a/src/conductor/client/orkes/orkes_secret_client.py b/src/conductor/client/orkes/orkes_secret_client.py index a3ee69f0..20868cd2 100644 --- a/src/conductor/client/orkes/orkes_secret_client.py +++ b/src/conductor/client/orkes/orkes_secret_client.py @@ -1,13 +1,11 @@ from typing import List from conductor.client.configuration.configuration import Configuration -from conductor.client.exceptions.api_exception_handler import api_exception_handler, for_all_methods from conductor.client.orkes.models.metadata_tag import MetadataTag from conductor.client.orkes.orkes_base_client import OrkesBaseClient from conductor.client.secret_client import SecretClient -@for_all_methods(api_exception_handler, ["__init__"]) class OrkesSecretClient(OrkesBaseClient, SecretClient): def __init__(self, configuration: Configuration): super(OrkesSecretClient, self).__init__(configuration) diff --git a/src/conductor/client/orkes/orkes_task_client.py b/src/conductor/client/orkes/orkes_task_client.py index d208b5c1..9f1ad3c9 100644 --- a/src/conductor/client/orkes/orkes_task_client.py +++ b/src/conductor/client/orkes/orkes_task_client.py @@ -1,7 +1,6 @@ from typing import Optional, List from conductor.client.configuration.configuration import Configuration -from conductor.client.exceptions.api_exception_handler import api_exception_handler, for_all_methods from conductor.client.http.models import PollData from conductor.client.http.models.task import Task from conductor.client.http.models.task_exec_log import TaskExecLog @@ -11,7 +10,6 @@ from conductor.client.task_client import TaskClient -@for_all_methods(api_exception_handler, ["__init__"]) class OrkesTaskClient(OrkesBaseClient, TaskClient): def __init__(self, configuration: Configuration): super(OrkesTaskClient, self).__init__(configuration) diff --git a/src/conductor/client/orkes/orkes_workflow_client.py b/src/conductor/client/orkes/orkes_workflow_client.py index fe1bdd90..d96dc51f 100644 --- a/src/conductor/client/orkes/orkes_workflow_client.py +++ b/src/conductor/client/orkes/orkes_workflow_client.py @@ -1,7 +1,6 @@ from typing import Optional, List from conductor.client.configuration.configuration import Configuration -from conductor.client.exceptions.api_exception_handler import api_exception_handler, for_all_methods from conductor.client.http.models import SkipTaskRequest, WorkflowStatus, \ ScrollableSearchResultWorkflowSummary from conductor.client.http.models.correlation_ids_search_request import CorrelationIdsSearchRequest @@ -14,7 +13,6 @@ from conductor.client.workflow_client import WorkflowClient -# @for_all_methods(api_exception_handler, ["__init__"]) class OrkesWorkflowClient(OrkesBaseClient, WorkflowClient): def __init__( self, diff --git a/tests/integration/client/orkes/test_orkes_clients.py b/tests/integration/client/orkes/test_orkes_clients.py index 6e1bbf71..1dab624b 100644 --- a/tests/integration/client/orkes/test_orkes_clients.py +++ b/tests/integration/client/orkes/test_orkes_clients.py @@ -3,6 +3,7 @@ from conductor.client.configuration.configuration import Configuration from conductor.client.http.api_client import ApiClient from conductor.client.http.models import SkipTaskRequest +from conductor.client.http.rest import ApiException from conductor.client.orkes_clients import OrkesClients from conductor.client.workflow.conductor_workflow import ConductorWorkflow from conductor.client.workflow.executor.workflow_executor import WorkflowExecutor @@ -22,7 +23,6 @@ from conductor.client.http.models.upsert_group_request import UpsertGroupRequest from conductor.client.http.models.create_or_update_application_request import CreateOrUpdateApplicationRequest from conductor.client.http.models.workflow_test_request import WorkflowTestRequest -from conductor.client.exceptions.api_error import APIError, APIErrorCode SUFFIX = str(uuid()) WORKFLOW_NAME = 'IntegrationTestOrkesClientsWf_' + SUFFIX @@ -103,8 +103,8 @@ def test_task_lifecycle(self): self.metadata_client.unregister_task_def(TASK_TYPE) try: self.metadata_client.get_task_def(TASK_TYPE) - except APIError as e: - assert e.code == APIErrorCode.NOT_FOUND + except ApiException as e: + assert e.code == 404 assert e.message == "Task {0} not found".format(TASK_TYPE) def test_secret_lifecycle(self): @@ -139,8 +139,8 @@ def test_secret_lifecycle(self): try: self.secret_client.get_secret(SECRET_NAME + "_2") - except APIError as e: - assert e.code == APIErrorCode.NOT_FOUND + except ApiException as e: + assert e.code == 404 def test_scheduler_lifecycle(self, workflowDef): startWorkflowRequest = StartWorkflowRequest( @@ -187,8 +187,8 @@ def test_scheduler_lifecycle(self, workflowDef): try: schedule = self.scheduler_client.get_schedule(SCHEDULE_NAME) - except APIError as e: - assert e.code == APIErrorCode.NOT_FOUND + except ApiException as e: + assert e.code == 404 assert e.message == "Schedule '{0}' not found".format(SCHEDULE_NAME) def test_application_lifecycle(self): @@ -237,8 +237,8 @@ def test_application_lifecycle(self): self.authorization_client.delete_application(created_app.id) try: application = self.authorization_client.get_application(created_app.id) - except APIError as e: - assert e.code == APIErrorCode.NOT_FOUND + except ApiException as e: + assert e.code == 404 assert e.message == "Application '{0}' not found".format(created_app.id) def test_user_group_permissions_lifecycle(self, workflowDef): @@ -314,15 +314,15 @@ def test_user_group_permissions_lifecycle(self, workflowDef): self.authorization_client.delete_user(USER_ID) try: self.authorization_client.get_user(USER_ID) - except APIError as e: - assert e.code == APIErrorCode.NOT_FOUND + except ApiException as e: + assert e.code == 404 assert e.message == "User '{0}' not found".format(USER_ID) self.authorization_client.delete_group(GROUP_ID) try: self.authorization_client.get_group(GROUP_ID) - except APIError as e: - assert e.code == APIErrorCode.NOT_FOUND + except ApiException as e: + assert e.code == 404 assert e.message == "Group '{0}' not found".format(GROUP_ID) def __test_register_workflow_definition(self, workflowDef: WorkflowDef): @@ -424,8 +424,8 @@ def __test_unregister_workflow_definition(self): try: self.metadata_client.get_workflow_def(WORKFLOW_NAME, 1) - except APIError as e: - assert e.code == APIErrorCode.NOT_FOUND + except ApiException as e: + assert e.code == 404 assert e.message == 'No such workflow found by name: {0}, version: 1'.format(WORKFLOW_NAME) def __test_task_tags(self): @@ -516,8 +516,8 @@ def __test_workflow_execution_lifecycle(self): self.workflow_client.delete_workflow(workflow_uuid) try: workflow = self.workflow_client.get_workflow(workflow_uuid, False) - except APIError as e: - assert e.code == APIErrorCode.NOT_FOUND + except ApiException as e: + assert e.code == 404 assert e.message == "Workflow with Id: {} not found.".format(workflow_uuid) def __test_task_execution_lifecycle(self):