polygon-io / client-js

The official JS client library for the Polygon REST and WebSocket API.
MIT License
176 stars 56 forks source link

noticing the JS api does not surface errors verbosely #194

Open rrshaban opened 1 month ago

rrshaban commented 1 month ago
const { restClient } = require('@polygon.io/client-js');
const rest = restClient("API KEY"); // actual code

rest.stocks.aggregates("AAPL", 1, "day", "2023-01-01", "2019-04-14").then((data) => {
    console.log(data);
}).catch(e => {
    console.error('An error happened:', e);
});

// last trade
rest.stocks.lastTrade("AAPL").then((data) => {
    console.log(data);
}).catch(e => {
    console.error('An error happened:', e);
});

// last quote (NBBO)
rest.stocks.lastQuote("AAPL").then((data) => {
    console.log(data);
}).catch(e => {
    console.error('An error happened:', e);
});

rest.stocks.snapshotAllTickers().then((data) => {
    console.log(data);
}).catch(e => {
    console.error('An error happened:', e);
});

(all of the above copy-paste from https://polygon.io/blog/javascript-stock-market-data)

% node p.js 
(node:1180) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
An error happened: T [Error]
    at u (/Users/razishaban/node_modules/@polygon.io/client-js/dist/main.cjs:1:1802)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  status: 'ERROR',
  request_id: 'bfeffa1a352ccd1d57303d10ca6002cb'
}
An error happened: T [Error]
    at u (/Users/razishaban/node_modules/@polygon.io/client-js/dist/main.cjs:1:1802)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  status: 'ERROR',
  request_id: 'd0470057a66487a9cd9a93c957cb6246'
}
An error happened: T [Error]
    at u (/Users/razishaban/node_modules/@polygon.io/client-js/dist/main.cjs:1:1802)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  status: 'ERROR',
  request_id: '7c98b5a795d7ad732cb5e3822da65ff7'
}
An error happened: T [Error]
    at u (/Users/razishaban/node_modules/@polygon.io/client-js/dist/main.cjs:1:1802)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  status: 'ERROR',
  request_id: 'ceffc82fc1a9dcffe5b6197c09a64ece'
}

It would be lovely if "T [Error]" offered some kind of ... error type . or message. as it is, the error was No api key, but how could I know?

noting that when I do add the ApiKey, I get 400s and 403... hmm...

NB: having tried 4 of the tutorials now, none of them seem to be "working as-is" with the beginner (Free) api key.

justinpolygon commented 1 month ago

Thanks @rrshaban. I noticed you were over in the client-go did you check out the examples in https://github.com/polygon-io/client-go/tree/master/rest/example? The readme in all the repos should work correctly. For example, the client-go one does https://github.com/polygon-io/client-go/tree/master.

I'll update the tutorials on the website to match since they have drifted over time. All the repos should be correct. On this repo, and the client-go, and client-python repos, can you enable tracing via https://github.com/polygon-io/client-go/tree/master and that will show you all the errors. It's the same concept but different syntax for each repo.

Also, what you're likely bumping up against is the 5 api call rate limit for the free tier.

peterrogov commented 1 month ago

I've been having the same issue. It appears that error handling code in the library is not matching the response structure from the API.

This line in the client core tries to use json.message to get the error message. https://github.com/polygon-io/client-js/blob/7fbbf6d191b663e342d95fb2e8df0514173fdeb1/src/rest/transport/request.ts#L95

However, according to the error response I am seeing in Postman, the correct field name to use is error

{
    "status": "ERROR",
    "request_id": "34f62761c946c5c8592b0fb35a6e156a",
    "error": "You've exceeded the maximum requests per minute, please wait or upgrade your subscription to continue. https://polygon.io/pricing"
}

Also, it would have been extremely helpful to have the HTTP response status code passed into StructuredError too.