Skip to content

Commit 655e321

Browse files
Release/7.0.2 (#133)
* Catch Rejected Promise and Throw Explicit Error (#132) * Catch promise rejection and re-throw. Tests for this scenario. * Use arrow functions in tests * 7.0.2 * Changelog
1 parent e5dec04 commit 655e321

File tree

8 files changed

+68
-18
lines changed

8 files changed

+68
-18
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## [7.0.2](https://github.com/polygon-io/client-js/v7.0.1...v7.0.2) (2023-02-15)
2+
3+
### Bug Fixes
4+
* Wrap fetch call in try/catch and bubble up network errors
5+
6+
## [6.2.1](https://github.com/polygon-io/client-js/v6.2.0...v6.2.1) (2023-02-15)
7+
8+
### Bug Fixes
9+
* Wrap fetch call in try/catch and bubble up network errors
10+
111
## [7.0.1](https://github.com/polygon-io/client-js/v7.0.0...v7.0.1) (2023-02-01)
212

313
### Bug Fixes

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@polygon.io/client-js",
3-
"version": "7.0.1",
3+
"version": "7.0.2",
44
"description": "Isomorphic Javascript client for Polygon.io Stocks, Forex, and Crypto APIs",
55
"main": "lib/main.js",
66
"types": "lib/main.d.ts",

src/rest/crypto/index.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,13 @@ describe("[REST] Crypto", () => {
207207
fetchStub.getCalls()[0].args[1].referrer.should.eql(mocks.overrideOptions.referrer);
208208
fetchStub.getCalls()[0].args[1].headers.header1.should.eql(mocks.globalOptions.headers.header1);
209209
});
210+
211+
it("properly handles promise rejections", async () => {
212+
fetchStub?.restore();
213+
fetchStub = sandbox.stub(fetchModule, 'fetch').rejects('Error Message')
214+
crypto = cryptoClient(mocks.key, mocks.base, mocks.globalOptions);
215+
return crypto.summaries({ 'ticker.any_of': "X:BTCUSD,X:USDBTC" })
216+
.then(() => { throw new Error('function should throw error'); })
217+
.catch((e) => { chai.expect(e).to.be.an.instanceof (Error); })
218+
});
210219
});

src/rest/forex/index.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,13 @@ describe("[REST] Forex / Currencies", () => {
196196
fetchStub.getCalls()[0].args[1].headers.header1.should.eql(mocks.globalOptions.headers.header1);
197197
});
198198

199+
it("properly handles promise rejections", async () => {
200+
fetchStub?.restore();
201+
fetchStub = sandbox.stub(fetchModule, 'fetch').rejects('Error Message')
202+
fx = forexClient(mocks.key, mocks.base, mocks.globalOptions);
203+
return fx.summaries({ 'ticker.any_of': "C:USDEUR,C:EURAUD" })
204+
.then(() => { throw new Error('function should throw error'); })
205+
.catch((e) => { chai.expect(e).to.be.an.instanceof (Error); })
206+
});
207+
199208
});

src/rest/options/index.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,13 @@ describe("[REST] Options", () => {
169169
fetchStub.getCalls()[0].args[1].referrer.should.eql(mocks.overrideOptions.referrer);
170170
fetchStub.getCalls()[0].args[1].headers.header1.should.eql(mocks.globalOptions.headers.header1);
171171
});
172+
173+
it("properly handles promise rejections", async () => {
174+
fetchStub?.restore();
175+
fetchStub = sandbox.stub(fetchModule, 'fetch').rejects('Error Message')
176+
options = optionsClient(mocks.key, mocks.base, mocks.globalOptions);
177+
return options.summaries({ 'ticker.any_of': "O:Ticker1,O:Ticker2" })
178+
.then(() => { throw new Error('function should throw error'); })
179+
.catch((e) => { chai.expect(e).to.be.an.instanceof (Error); })
180+
});
172181
});

src/rest/stocks/index.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,13 @@ describe("[REST] Stocks", () => {
208208
fetchStub.getCalls()[0].args[1].referrer.should.eql(mocks.overrideOptions.referrer);
209209
fetchStub.getCalls()[0].args[1].headers.header1.should.eql(mocks.globalOptions.headers.header1);
210210
});
211+
212+
it("properly handles promise rejections", async () => {
213+
fetchStub?.restore();
214+
fetchStub = sandbox.stub(fetchModule, 'fetch').rejects('Error Message')
215+
stocks = stocksClient(mocks.key, mocks.base, mocks.globalOptions);
216+
return stocks.summaries({ 'ticker.any_of': "AAPL,TSLA" })
217+
.then(() => { throw new Error('function should throw error'); })
218+
.catch((e) => { chai.expect(e).to.be.an.instanceof (Error); })
219+
});
211220
});

src/rest/transport/request.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,27 @@ export const getWithGlobals: ICurriedGet = (apiKey, apiBase, globalOptions = {})
3434

3535
const queryString = stringify(query, { encode: true });
3636
const url = `${apiBase}${path}?${queryString}`;
37-
const response = await fetchModule.fetch(url, {
38-
...globalOptions,
39-
...options,
40-
headers: {
41-
...(options.headers || globalOptions.headers || {}),
42-
"Authorization": `Bearer ${apiKey}`
37+
try {
38+
const response = await fetchModule.fetch(url, {
39+
...globalOptions,
40+
...options,
41+
headers: {
42+
...(options.headers || globalOptions.headers || {}),
43+
"Authorization": `Bearer ${apiKey}`
44+
}
45+
});
46+
47+
if (response.status >= 400) {
48+
const message = await response.text();
49+
throw new Error(message);
4350
}
44-
});
4551

46-
if (response.status >= 400) {
47-
const message = await response.text();
48-
throw new Error(message);
49-
}
52+
if (response?.headers?.get('content-type') === 'text/csv') {
53+
return response.text();
54+
}
5055

51-
if (response?.headers?.get('content-type') === 'text/csv') {
52-
return response.text();
56+
return response.json();
57+
} catch (e) {
58+
throw new Error(e);
5359
}
54-
55-
return response.json();
5660
};

0 commit comments

Comments
 (0)