Skip to content

Commit

Permalink
Error handling fix for JSONDecodeError on adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
chandrasekharan-zipstack committed Jul 18, 2024
1 parent 9e27a4e commit f9b465b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
7 changes: 7 additions & 0 deletions backend/adapter_processor/adapter_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
InternalServiceError,
InValidAdapterId,
TestAdapterError,
TestAdapterInputError,
)
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
Expand Down Expand Up @@ -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))

Expand Down
9 changes: 6 additions & 3 deletions backend/adapter_processor/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from rest_framework.exceptions import APIException

from backend.exceptions import UnstractBaseException


class IdIsMandatory(APIException):
status_code = 400
Expand Down Expand Up @@ -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

Expand Down
18 changes: 7 additions & 11 deletions backend/adapter_processor/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
30 changes: 15 additions & 15 deletions backend/connector_processor/connector_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f9b465b

Please sign in to comment.