Skip to content

Commit

Permalink
Enrich the error messga for the LLM when the GADs account isn't activ…
Browse files Browse the repository at this point in the history
…ated or it is deleted
  • Loading branch information
rjambrecic committed May 14, 2024
1 parent 293db19 commit 5a9d1ca
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
11 changes: 11 additions & 0 deletions captn/google_ads/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ def clean_error_response(content: bytes) -> str:


AUTHENTICATION_ERROR = "Please try to execute the command again."
ACCOUNT_NOT_ACTIVATED = (
"be accessed because it is not yet enabled or has been deactivated"
)


def execute_query(
Expand Down Expand Up @@ -106,6 +109,14 @@ def execute_query(
content = AUTHENTICATION_ERROR
else:
content = clean_error_response(response.content)
if ACCOUNT_NOT_ACTIVATED in content:
content = f"""We have received the following error from Google Ads API:
{content}
If you have just created the account, please wait for a few hours before trying again.
If the account has been active for a while, please check the account status in the Google Ads UI.
"""
raise ValueError(content)

response_json = response.json()
Expand Down
26 changes: 26 additions & 0 deletions tests/ci/test_client.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import unittest
from typing import List, Optional, Tuple

import pytest
from pydantic import BaseModel
from requests.models import Response

from captn.google_ads.client import (
ALREADY_AUTHENTICATED,
FIELDS_ARE_NOT_MENTIONED_ERROR_MSG,
NOT_APPROVED,
NOT_IN_QUESTION_ANSWER_LIST,
_check_for_client_approval,
check_fields_are_mentioned_to_the_client,
clean_error_response,
execute_query,
google_ads_create_update,
)

Expand Down Expand Up @@ -143,3 +147,25 @@ def test_google_ads_create_update_raises_error() -> None:
+ "\n\n"
+ NOT_IN_QUESTION_ANSWER_LIST
)


def test_execute_query_when_acount_isnt_acivated() -> None:
with (
unittest.mock.patch(
"captn.google_ads.client.get_login_url",
return_value={"login_url": ALREADY_AUTHENTICATED},
),
unittest.mock.patch(
"captn.google_ads.client.requests_get",
) as mock_requests_get,
):
response = Response()
response.status_code = 500
response._content = b'{"detail":"(<_InactiveRpcError of RPC that terminated with:\\n\\tstatus = StatusCode.PERMISSION_DENIED\\n\\tdetails = \\"The caller does not have permission\\"\\n\\tdebug_error_string = \\"UNKNOWN:Error received from peer {created_time:\\"2024-05-14T10:33:11\\", grpc_status:7, grpc_message:\\"The caller does not have permission\\"}\\"\\n>, <_InactiveRpcError of RPC that terminated with:\\n\\tstatus = StatusCode.PERMISSION_DENIED\\n\\tdetails = \\"The caller does not have permission\\"\\n\\tdebug_error_string = \\"UNKNOWN:Error received from peer ipv4:142.250.184.138:443 {created_time:\\"2024-05-14T10:33:11\\", grpc_status:7, grpc_message:\\"The caller does not have permission\\"}\\"\\n>, errors {\\n error_code {\\n authorization_error: CUSTOMER_NOT_ENABLED\\n }\\n message: \\"The customer account can\\\\\'t be accessed because it is not yet enabled or has been deactivated.\\"})"}'
mock_requests_get.return_value = response

with pytest.raises(ValueError) as exc_info:
execute_query(user_id=-1, conv_id=-1)

excepted_substr = "If you have just created the account, please wait for a few hours before trying again."
assert excepted_substr in exc_info.value.args[0], exc_info.value.args[0]

0 comments on commit 5a9d1ca

Please sign in to comment.