From 5a9d1ca0944c8dc58a48b22c0f1d50934e19ed22 Mon Sep 17 00:00:00 2001 From: Robert Jambrecic Date: Tue, 14 May 2024 10:56:10 +0200 Subject: [PATCH] Enrich the error messga for the LLM when the GADs account isn't activated or it is deleted --- captn/google_ads/client.py | 11 +++++++++++ tests/ci/test_client.py | 26 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/captn/google_ads/client.py b/captn/google_ads/client.py index 7aef6a0b..8e02955f 100644 --- a/captn/google_ads/client.py +++ b/captn/google_ads/client.py @@ -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( @@ -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() diff --git a/tests/ci/test_client.py b/tests/ci/test_client.py index 8eb5da31..c11f9a30 100644 --- a/tests/ci/test_client.py +++ b/tests/ci/test_client.py @@ -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, ) @@ -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]