Skip to content

Commit

Permalink
fix: remove infinite loop 422
Browse files Browse the repository at this point in the history
  • Loading branch information
ariadna-roman committed Jun 20, 2023
1 parent 00a585e commit 8628cbc
Showing 1 changed file with 10 additions and 31 deletions.
41 changes: 10 additions & 31 deletions src/lib/api/poll-import/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'source-map-support/register';
import * as url from 'url';
import { requestsManager } from 'snyk-request-manager';
import type { requestsManager } from 'snyk-request-manager';
import * as sleep from 'sleep-promise';
import * as debugLib from 'debug';
import * as _ from 'lodash';
Expand Down Expand Up @@ -33,28 +33,25 @@ export async function pollImportUrl(
}
try {
const { pathname = '' } = url.parse(locationUrl);
const res = await tryPolling(
requestManager,
(pathname as string).split('/api/v1/')[1],
);
const res = await requestManager.request({
verb: 'get',
url: (pathname as string).split('/api/v1/')[1],
body: JSON.stringify({}),
});
const importStatus: PollImportResponse = res.data;
const statusCode = res.statusCode || res.status;

if (!statusCode || ![422, 200].includes(statusCode)) {
if (!statusCode || statusCode !== 200) {
throw new Error(
'Expected a 200 response, instead received: ' +
JSON.stringify({ data: res.data, status: statusCode }),
);
}
if (statusCode == 422) {
retryCount = 3;
}
debug(`Import task status is "${importStatus.status}"`);
if (
statusCode == 422 ||
(importStatus.status &&
importStatus.status !== 'complete' &&
retryCount > 0)
importStatus.status &&
importStatus.status !== 'complete' &&
retryCount > 0
) {
await sleep(retryWaitTime);
debug(`Will re-check import task in "${retryWaitTime} ms"`);
Expand All @@ -79,24 +76,6 @@ export async function pollImportUrl(
}
}

async function tryPolling(requestManager: requestsManager, pollingUrl: string) {
try {
const res = await requestManager.request({
verb: 'get',
url: pollingUrl,
body: JSON.stringify({}),
});

return res;
} catch (e) {
if (e.message?.response?.status == 422) {
return e.message?.response;
}

throw e;
}
}

export async function pollImportUrls(
requestManager: requestsManager,
locationUrls: string[],
Expand Down

0 comments on commit 8628cbc

Please sign in to comment.