feat: Handle Google Ads EU Political Advertising Declaration error #747
feat: Handle Google Ads EU Political Advertising Declaration error #747Itzaprado wants to merge 2 commits into
Conversation
- Fix async bug in GoogleAdsApiClient where apiCall was not awaited in try-catch, causing errors to bypass handleEuPoliticalError. - Fix error overwrite bug in makeBetterErrorMessage where customized error messages were lost. - Add test case in test_customer_match.ts to verify the EU political ad error handling flow. TAG=agy
There was a problem hiding this comment.
Code Review
This pull request introduces custom error handling for the Google Ads API error EU_POLITICAL_ADVERTISING_DECLARATION_REQUIRED, providing a clear, actionable error message for EU political advertising requirements. It also updates error utilities to append webhook IDs to this error and adds corresponding unit tests. The review feedback suggests centralizing the error handling logic inside the apiCall method to avoid duplicating try/catch blocks across multiple API methods, and using optional chaining when checking the error name in makeBetterErrorMessage to prevent potential runtime errors.
| try { | ||
| return await this.apiCall(method, path, body) | ||
| } catch (error) { | ||
| this.handleEuPoliticalError(error) | ||
| throw error | ||
| } |
There was a problem hiding this comment.
Instead of duplicating the try/catch block and handleEuPoliticalError call in every API method (like createUserList and createDataJob), it is much cleaner and more maintainable to centralize this error handling inside the apiCall method itself. This avoids code duplication and ensures that all API calls (including addDataJobOperations, runJob, and any future methods) automatically benefit from the EU political advertising error interceptor.
return this.apiCall(method, path, body)| try { | ||
| return await this.apiCall(method, path, body) | ||
| } catch (error) { | ||
| this.handleEuPoliticalError(error) | ||
| throw error | ||
| } |
| if (err.name === "EU Political Advertising Error") { | ||
| if (webhookId) { | ||
| err.message = err.message + ` (Webhook ID: ${webhookId})` | ||
| } | ||
| return | ||
| } |
There was a problem hiding this comment.
To prevent potential runtime TypeErrors if err is null or undefined, use optional chaining (err?.name) to safely check the error name.
| if (err.name === "EU Political Advertising Error") { | |
| if (webhookId) { | |
| err.message = err.message + ` (Webhook ID: ${webhookId})` | |
| } | |
| return | |
| } | |
| if (err?.name === "EU Political Advertising Error") { | |
| if (webhookId) { | |
| err.message = err.message + " (Webhook ID: " + webhookId + ")" | |
| } | |
| return | |
| } |
Description
This PR implements user-friendly error handling for the Google Ads API error
EU_POLITICAL_ADVERTISING_DECLARATION_REQUIRED.According to Google Ads API Policy, advertisers who wish to show political ads in the EU must complete identity verification and declare their intent in the Google Ads UI. If they attempt to use Customer Match without doing so, the API returns this specific error.
Instead of displaying a generic "Request contains an invalid argument" error, this change intercepts the error and provides actionable guidance to the user, redirecting them to the verification steps.
Technical Changes
GoogleAdsApiClientwhereapiCallpromises were not beingawaited within thetry/catchblock, causing API errors to bypass the local error handler."EU Political Advertising Error".makeBetterErrorMessageinerror_utils.tsto recognize this tagged error and return early, preventing the custom message from being overwritten by generic API error details.test_customer_match.tsthat mocks this API error and verifies the end-to-end integration and the final error message structure.Reference Documentation