Skip to content

Commit

Permalink
fix error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
v1r3n committed Dec 29, 2023
1 parent 495c657 commit e9475a8
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 45 deletions.
20 changes: 9 additions & 11 deletions src/conductor/client/exceptions/api_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 0 additions & 1 deletion src/conductor/client/exceptions/api_exception_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/conductor/client/http/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 0 additions & 2 deletions src/conductor/client/orkes/orkes_authorization_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions src/conductor/client/orkes/orkes_integration_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
2 changes: 0 additions & 2 deletions src/conductor/client/orkes/orkes_metadata_client.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions src/conductor/client/orkes/orkes_prompt_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
2 changes: 0 additions & 2 deletions src/conductor/client/orkes/orkes_scheduler_client.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions src/conductor/client/orkes/orkes_secret_client.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 0 additions & 2 deletions src/conductor/client/orkes/orkes_task_client.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions src/conductor/client/orkes/orkes_workflow_client.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down
34 changes: 17 additions & 17 deletions tests/integration/client/orkes/test_orkes_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit e9475a8

Please sign in to comment.