Skip to content

Commit

Permalink
Merge pull request #806 from DalgoT4D/799-return-connection-error-fro…
Browse files Browse the repository at this point in the history
…m-airbyte

getting the failure reason from the api and sending external message …
  • Loading branch information
fatchat authored Aug 9, 2024
2 parents 7a61479 + bfa3a75 commit 1d6f197
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 30 deletions.
20 changes: 12 additions & 8 deletions ddpui/ddpairbyte/airbyte_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,9 @@ def check_source_connection(workspace_id: str, data: AirbyteSourceCreate) -> dic
timeout=60,
)
if "jobInfo" not in res or res.get("status") == "failed":
logger.error("Failed to check source connection: %s", res)
raise HttpError(500, "Failed to connect - please check your crendentials")
failure_reason = res.get("message", "Something went wrong, please check your credentials")
logger.error("Failed to check the source connection: %s", res)
raise HttpError(500, failure_reason)
return res


Expand All @@ -419,8 +420,9 @@ def check_source_connection_for_update(
timeout=60,
)
if "jobInfo" not in res or res.get("status") == "failed":
logger.error("Failed to check source connection: %s", res)
raise HttpError(500, "Failed to connect - please check your crendentials")
failure_reason = res.get("message", "Something went wrong, please check your credentials")
logger.error("Failed to check the source connection: %s", res)
raise HttpError(500, failure_reason)
# {
# 'status': 'succeeded',
# 'jobInfo': {
Expand Down Expand Up @@ -645,8 +647,9 @@ def check_destination_connection(
timeout=60,
)
if "jobInfo" not in res or res.get("status") == "failed":
logger.error("Failed to check destination connection: %s", res)
raise HttpError(500, "Failed to connect - please check your crendentials")
failure_reason = res.get("message", "Something went wrong, please check your credentials")
logger.error("Failed to check the destination connection: %s", res)
raise HttpError(500, failure_reason)
return res


Expand All @@ -667,8 +670,9 @@ def check_destination_connection_for_update(
timeout=60,
)
if "jobInfo" not in res or res.get("status") == "failed":
logger.error("Failed to check destination connection: %s", res)
raise HttpError(500, "Failed to connect - please check your crendentials")
failure_reason = res.get("message", "Something went wrong, please check your credentials")
logger.error("Failed to check the destination connection: %s", res)
raise HttpError(500, failure_reason)
return res


Expand Down
84 changes: 62 additions & 22 deletions ddpui/tests/services/test_airbyte_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,6 @@ def test_check_source_connection_success():
assert result == expected_response_src
assert isinstance(result, dict)


def test_check_source_connection_failure():
workspace_id = "my_workspace_id"
data = AirbyteSourceCreate(
Expand All @@ -658,14 +657,23 @@ def test_check_source_connection_failure():
config={"key": "value"},
)
expected_response_srcdef = {"connectionSpecification": {"properties": {}}}
failed_response = {
"status": "failed",
"jobInfo": {
"succeeded": False,
},
"message": "Credentials are invalid"
}

with patch("ddpui.ddpairbyte.airbyte_service.abreq") as mock_abreq_:
mock_abreq_.side_effect = [expected_response_srcdef, {}]
mock_abreq_.side_effect = [expected_response_srcdef, failed_response]

with pytest.raises(HttpError) as excinfo:
check_source_connection(workspace_id, data)
assert (
str(excinfo.value)
== "Failed to connect - please check your crendentials"
)

assert str(excinfo.value) == "Credentials are invalid"




def test_check_source_connection_with_invalid_workspace_id():
Expand Down Expand Up @@ -1149,14 +1157,23 @@ def test_check_destination_connection_failure_1():
mock_response = Mock(spec=requests.Response)
mock_response.status_code = 200
mock_response.headers = {"Content-Type": "application/json"}
mock_response.json.return_value = {"wrong-key": "theConnectionSpecification"}
mock_response.json.return_value = {
"status": "failed",
"message": "Credentials are invalid",
"jobInfo": {
"succeeded": False,
}
}
mock_post.return_value = mock_response

with pytest.raises(HttpError) as excinfo:
check_destination_connection("workspace_id", payload)

assert (
str(excinfo.value) == "Failed to connect - please check your crendentials"
)
assert str(excinfo.value) == "Credentials are invalid"






def test_check_destination_connection_failure_2():
Expand All @@ -1167,14 +1184,21 @@ def test_check_destination_connection_failure_2():
mock_response = Mock(spec=requests.Response)
mock_response.status_code = 200
mock_response.headers = {"Content-Type": "application/json"}
mock_response.json.return_value = {"jobInfo": {}, "status": "failed"}
mock_response.json.return_value = {
"status": "failed",
"message": "Credentials are invalid",
"jobInfo": {
"succeeded": False,
}
}
mock_post.return_value = mock_response

with pytest.raises(HttpError) as excinfo:
check_destination_connection("workspace_id", payload)

assert (
str(excinfo.value) == "Failed to connect - please check your crendentials"
)
assert str(excinfo.value) == "Credentials are invalid"




def test_check_destination_connection_for_update_success():
Expand Down Expand Up @@ -1203,14 +1227,21 @@ def test_check_destination_connection_for_update_failure_1():
mock_response = Mock(spec=requests.Response)
mock_response.status_code = 200
mock_response.headers = {"Content-Type": "application/json"}
mock_response.json.return_value = {"wrong-key": "theConnectionSpecification"}
mock_response.json.return_value = {
"status": "failed",
"message": "Credentials are invalid",
"jobInfo": {
"succeeded": False,
}
}
mock_post.return_value = mock_response

with pytest.raises(HttpError) as excinfo:
check_destination_connection_for_update("destination_id", payload)

assert (
str(excinfo.value) == "Failed to connect - please check your crendentials"
)
assert str(excinfo.value) == "Credentials are invalid"




def test_check_destination_connection_for_update_failure_2():
Expand All @@ -1219,14 +1250,23 @@ def test_check_destination_connection_for_update_failure_2():
mock_response = Mock(spec=requests.Response)
mock_response.status_code = 200
mock_response.headers = {"Content-Type": "application/json"}
mock_response.json.return_value = {"jobInfo": {}, "status": "failed"}
mock_response.json.return_value = {
"status": "failed",
"message": "Credentials are invalid",
"jobInfo": {
"succeeded": False,
}
}
mock_post.return_value = mock_response

with pytest.raises(HttpError) as excinfo:
check_destination_connection_for_update("destination_id", payload)

assert (
str(excinfo.value) == "Failed to connect - please check your crendentials"
)
assert str(excinfo.value) == "Credentials are invalid"






def test_get_connections_bad_workspace_id():
Expand Down

0 comments on commit 1d6f197

Please sign in to comment.