Opteo / google-ads-api

Google Ads API client library for Node.js
https://opteo.com
MIT License
264 stars 88 forks source link

API should handle 404 errors correctly #500

Open simplenotezy opened 2 weeks ago

simplenotezy commented 2 weeks ago

With the following code const campaigns = await customer.query( it fails with error:

Error: Parser cannot parse input: expected a value
    at Parser._processBuffer (/Users/me/Projects/project/backend/node_modules/.pnpm/stream-json@1.8.0/node_modules/stream-json/Parser.js:111:64)
    at Parser._transformBuffer (/Users/me/Projects/project/backend/node_modules/.pnpm/stream-json@1.8.0/node_modules/stream-json/utils/Utf8Stream.js:24:10)
    at Parser._transform (/Users/me/Projects/project/backend/node_modules/.pnpm/stream-json@1.8.0/node_modules/stream-json/utils/Utf8Stream.js:19:10)
    at Parser.Transform._write (node:internal/streams/transform:171:8)
    at writeOrBuffer (node:internal/streams/writable:564:12)
    at _write (node:internal/streams/writable:493:10)
    at Parser.Writable.write (node:internal/streams/writable:502:10)
    at IncomingMessage.ondata (node:internal/streams/readable:1007:22)
    at IncomingMessage.emit (node:events:518:28)
    at IncomingMessage.emit (node:domain:488:12) {stack: 'Error: Parser cannot parse input: expected a … at IncomingMessage.emit (node:domain:488:12)', message: 'Parser cannot parse input: expected a value'}

Full code example:

    const customer = client.Customer({
      customer_id: 'customers/XXX',
      refresh_token: credentials.key,
    });

    try {
      const campaigns = await customer.query(`
                SELECT 
                    campaign.id, 
                    campaign.name
                FROM 
                    campaign
            `);
    } catch (error) {
      console.error(error);
    }

It appears to receive a 404 when issuing a request towards this URL:

https://googleads.googleapis.com/v16/customers/customers/XXX/googleAds:searchStream

When removing the customers/ prefix, the query works as intended.

Possible solutions:

  1. Properly handle 404 errors
  2. Automatically remove customers/ prefix from customer_id
simplenotezy commented 2 weeks ago

OK - found the problem:

The customer ID should be without the customer/ prefix. I was initially simply looping through the return of const customerIds = (await client.listAccessibleCustomers(credentials.key)).resource_names which automatically included the prefix. When the API returns a 404 the library cannot properly handle the error message, and that's when the Parser cannot parse input: expected a value error occours

The library should properly handle 404 errors from the API