From f9b465bcc984dc2a9e4699101f2480978cb70601 Mon Sep 17 00:00:00 2001 From: Chandrasekharan M Date: Thu, 18 Jul 2024 14:48:51 +0530 Subject: [PATCH] Error handling fix for JSONDecodeError on adapters --- .../adapter_processor/adapter_processor.py | 7 +++++ backend/adapter_processor/exceptions.py | 9 ++++-- backend/adapter_processor/views.py | 18 +++++------ .../connector_processor.py | 30 +++++++++---------- 4 files changed, 35 insertions(+), 29 deletions(-) diff --git a/backend/adapter_processor/adapter_processor.py b/backend/adapter_processor/adapter_processor.py index f8c4bb1ef..786280367 100644 --- a/backend/adapter_processor/adapter_processor.py +++ b/backend/adapter_processor/adapter_processor.py @@ -8,6 +8,7 @@ InternalServiceError, InValidAdapterId, TestAdapterError, + TestAdapterInputError, ) from django.conf import settings from django.core.exceptions import ObjectDoesNotExist @@ -97,6 +98,12 @@ def test_adapter(adapter_id: str, adapter_metadata: dict[str, Any]) -> bool: test_result: bool = adapter_instance.test_connection() logger.info(f"{adapter_id} test result: {test_result}") return test_result + # HACK: Remove after error is explicitly handled in VertexAI adapter + except json.JSONDecodeError: + raise TestAdapterInputError( + "Credentials is not a valid service account JSON, " + "please provide a valid JSON." + ) except AdapterError as e: raise TestAdapterError(str(e)) diff --git a/backend/adapter_processor/exceptions.py b/backend/adapter_processor/exceptions.py index 5bddbb3c2..5e1bbf948 100644 --- a/backend/adapter_processor/exceptions.py +++ b/backend/adapter_processor/exceptions.py @@ -2,8 +2,6 @@ from rest_framework.exceptions import APIException -from backend.exceptions import UnstractBaseException - class IdIsMandatory(APIException): status_code = 400 @@ -46,11 +44,16 @@ class UniqueConstraintViolation(APIException): default_detail = "Unique constraint violated" -class TestAdapterError(UnstractBaseException): +class TestAdapterError(APIException): status_code = 500 default_detail = "Error while testing adapter" +class TestAdapterInputError(APIException): + status_code = 400 + default_detail = "Error while testing adapter, please check the configuration." + + class DeleteAdapterInUseError(APIException): status_code = 409 diff --git a/backend/adapter_processor/views.py b/backend/adapter_processor/views.py index 333b05d71..162e4aaf0 100644 --- a/backend/adapter_processor/views.py +++ b/backend/adapter_processor/views.py @@ -115,17 +115,13 @@ def test(self, request: Request) -> Response: adapter_metadata[AdapterKeys.ADAPTER_TYPE] = serializer.validated_data.get( AdapterKeys.ADAPTER_TYPE ) - try: - test_result = AdapterProcessor.test_adapter( - adapter_id=adapter_id, adapter_metadata=adapter_metadata - ) - return Response( - {AdapterKeys.IS_VALID: test_result}, - status=status.HTTP_200_OK, - ) - except Exception as e: - logger.error(f"Error testing adapter : {str(e)}") - raise e + test_result = AdapterProcessor.test_adapter( + adapter_id=adapter_id, adapter_metadata=adapter_metadata + ) + return Response( + {AdapterKeys.IS_VALID: test_result}, + status=status.HTTP_200_OK, + ) class AdapterInstanceViewSet(ModelViewSet): diff --git a/backend/connector_processor/connector_processor.py b/backend/connector_processor/connector_processor.py index b2346a9d4..e014d6979 100644 --- a/backend/connector_processor/connector_processor.py +++ b/backend/connector_processor/connector_processor.py @@ -8,7 +8,6 @@ from connector_auth.pipeline.common import ConnectorAuthHelper from connector_processor.constants import ConnectorKeys from connector_processor.exceptions import ( - InternalServiceError, InValidConnectorId, InValidConnectorMode, OAuthTimeOut, @@ -45,26 +44,27 @@ def get_json_schema(connector_id: str) -> dict: updated_connectors = fetch_connectors_by_key_value( ConnectorKeys.ID, connector_id ) - if len(updated_connectors) != 0: - connector = updated_connectors[0] - schema_details[ConnectorKeys.OAUTH] = connector.get(ConnectorKeys.OAUTH) - schema_details[ConnectorKeys.SOCIAL_AUTH_URL] = connector.get( - ConnectorKeys.SOCIAL_AUTH_URL - ) - try: - schema_details[ConnectorKeys.JSON_SCHEMA] = json.loads( - connector.get(ConnectorKeys.JSON_SCHEMA) - ) - except Exception as exc: - logger.error(f"Error occurred while parsing JSON Schema: {exc}") - raise InternalServiceError() - else: + if len(updated_connectors) == 0: logger.error( f"Invalid connector Id : {connector_id} " f"while fetching " f"JSON Schema" ) raise InValidConnectorId() + + connector = updated_connectors[0] + schema_details[ConnectorKeys.OAUTH] = connector.get(ConnectorKeys.OAUTH) + schema_details[ConnectorKeys.SOCIAL_AUTH_URL] = connector.get( + ConnectorKeys.SOCIAL_AUTH_URL + ) + try: + schema_details[ConnectorKeys.JSON_SCHEMA] = json.loads( + connector.get(ConnectorKeys.JSON_SCHEMA) + ) + except Exception as exc: + logger.error(f"Error occurred decoding JSON for {connector_id}: {exc}") + raise exc + return schema_details @staticmethod