Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API should handle 404 errors correctly #500

Open
simplenotezy opened this issue Aug 28, 2024 · 2 comments
Open

API should handle 404 errors correctly #500

simplenotezy opened this issue Aug 28, 2024 · 2 comments

Comments

@simplenotezy
Copy link

simplenotezy commented Aug 28, 2024

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
Copy link
Author

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

@simplenotezy simplenotezy changed the title Error: Parser cannot parse input: expected a value API should handle 404 errors correctly Aug 28, 2024
@visrut-at-handldigital
Copy link

Thanks, @simplenotezy, for the solution you found. Yes, the error is not handled properly after console logging inside node_modules. I got to know something was 404 but didn't know the reason. After your suggestion, the below code worked for me.

const client = new GoogleAdsApi({
  client_id: process.env.GOOGLE_ADWORDS_CLIENT_ID!,
  client_secret: process.env.GOOGLE_ADWORDS_CLIENT_SECRET!,
  developer_token: process.env.GOOGLE_ADWORDS_DEVELOPER_TOKEN!,
});

const customerIds = (await client.listAccessibleCustomers(refreshToken)).resource_names;

const customer = client.Customer({
  customer_id: customerIds[0].replace('customers/', ''),
  refresh_token: refreshToken,
});

A good error message would help since I overlooked the docs and don't seem to notice that we only need to pass the ID, not the customers/ prefix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants