arifulhb / amazon-pa-api50

Amazon Product Advertising API v5.0 for JavaScript and NodeJs
https://www.npmjs.com/package/amazon-pa-api50
MIT License
28 stars 9 forks source link

Unexpected token u in JSON at position 0 #4

Closed Kinzi closed 4 years ago

Kinzi commented 4 years ago

I'm not 100% when this happens but it's probably on an empty or faulty response from the API:

SyntaxError: Unexpected token u in JSON at position 0
     at JSON.parse (<anonymous>)
     at /app/node_modules/amazon-pa-api50/src/index.js:181:38
    at /app/node_modules/amazon-pa-api50/sdk/src/ApiClient.js:504:9

It happens in those lines when searchItemsResponse is undefined:

https://github.com/arifulhb/amazon-pa-api50/blob/d089c45e9d83ef6d5abcf392507eb0bd8e7db2fc/src/index.js#L180-L181

So I guess it's either checking if searchItemsResponse actually is a JSON object or making sure ProductAdvertisingAPIv1.SearchItemsResponse.constructFromObject(data)does not return anything else.

Kinzi commented 4 years ago

Seems like this happens when you hit the request limit of the API.

MaiKaY commented 4 years ago

Hi @Kinzi, I got the same error. Did you find a way to solve or at least try/catch it?

Kinzi commented 4 years ago

@MaiKaY Check out my fork. Not super elegant but at least it allows me to catch the error:

https://github.com/Kinzi/amazon-pa-api50

@arifulhb Happy to make a pull request out of this if you like it ;)

arifulhb commented 4 years ago

Hay, @Kinzi great to know you had time to fix the issue. Please send the PR and I'll just marge it.

Kinzi commented 4 years ago

@arifulhb @MaiKaY already did ;)