Skip to content

Commit

Permalink
fix(iows): propagate timeout errors (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
gitolicious authored Jan 19, 2022
1 parent 2b6da10 commit 9023f54
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
3 changes: 3 additions & 0 deletions source/cli-stock.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ Examples:
if (err instanceof errors.IOWS2DeprecatedError) {
return { stock: 0, probability: 'DEPRECATED', createdAt: new Date() };
}
if (err.code === 'ECONNABORTED') {
return { stock: 0, probability: 'TIMEOUT', createdAt: new Date() };
}
throw err;
})
.then((availability) => ({
Expand Down
4 changes: 2 additions & 2 deletions source/lib/iows2.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class IOWS2 {
try {
response = await this.api.get(url, options);
} catch (error) {
if (error.request) {
if (error.request.res) {
// 20211217 some of the country endpoints started to reply with a
// deprecation and warning header stating that the IOWS2 API is
// going to be deprecated.
Expand Down Expand Up @@ -229,7 +229,7 @@ class IOWS2 {
const url = this.buildUrl(this.baseUrl, this.countryCode, this.languageCode, buCode, productId, productType);
return this.fetch(url)
.catch(err => {
if (err.request.res) {
if (err.request?.res) {
err.message =
`Unable to receive product ${productId} availability for store ` +
`${buCode} status code: ${err.request.res.statusCode} ${err.request.res.statusText} ` +
Expand Down
12 changes: 12 additions & 0 deletions source/lib/iows2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,18 @@ describe('IOWS2', () => {
});
});

it('propagates a timeout error when the api is not responding', () => {
const scope = nock(URL).get(/.+/)
.delayConnection(50)
.reply(200);
return iows.fetch(URL, {timeout: 10}) // reduce timeout to keep test duration low
.catch(err => {
expect(err).to.be.instanceof(errors.IOWS2ResponseError);
expect(err.code).to.equal('ECONNABORTED');
scope.isDone();
});
});

it('sends the correct header and request data', () => {
const scope = nock(URL, {
reqheaders: {
Expand Down

0 comments on commit 9023f54

Please sign in to comment.