Skip to content

Commit 07ed532

Browse files
committed
524 fix
1 parent d5e8188 commit 07ed532

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

lib/commands/info/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function setupCommand (name, description, argv, importMeta) {
126126
async function fetchPackageData (pkgName, pkgVersion, { includeAllIssues, strict }) {
127127
const socketSdk = await setupSdk()
128128
const spinner = ora(`Looking up data for version ${pkgVersion} of ${pkgName}`).start()
129-
const result = await handleApiCall(socketSdk.getIssuesByNPMPackage(pkgName, pkgVersion), spinner, 'looking up package')
129+
const result = await handleApiCall(socketSdk.getIssuesByNPMPackage(pkgName, pkgVersion), 'looking up package')
130130

131131
if (result.success === false) {
132132
return handleUnsuccessfulApiResponse('getIssuesByNPMPackage', result, spinner)

lib/commands/report/create.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ async function createReport (packagePaths, { config, cwd, debugLog, dryRun }) {
220220
const socketSdk = await setupSdk()
221221
const spinner = ora(`Creating report with ${packagePaths.length} package files`).start()
222222
const apiCall = socketSdk.createReportFromFilePaths(packagePaths, cwd, config?.issueRules)
223-
const result = await handleApiCall(apiCall, spinner, 'creating report')
223+
const result = await handleApiCall(apiCall, 'creating report')
224224

225225
if (result.success === false) {
226226
return handleUnsuccessfulApiResponse('createReport', result, spinner)

lib/commands/report/view.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import chalk from 'chalk'
44
import meow from 'meow'
55
import ora from 'ora'
6+
import { ErrorWithCause } from 'pony-cause'
67

78
import { outputFlags, validationFlags } from '../../flags/index.js'
89
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js'
@@ -102,6 +103,8 @@ function setupCommand (name, description, argv, importMeta) {
102103
* @typedef {import('@socketsecurity/sdk').SocketSdkReturnType<'getReport'>["data"]} ReportData
103104
*/
104105

106+
const MAX_TIMEOUT_RETRY = 5
107+
105108
/**
106109
* @param {string} reportId
107110
* @param {Pick<CommandContext, 'includeAllIssues' | 'strict'>} context
@@ -111,8 +114,22 @@ export async function fetchReportData (reportId, { includeAllIssues, strict }) {
111114
// Do the API call
112115

113116
const socketSdk = await setupSdk()
114-
const spinner = ora(`Fetching report with ID ${reportId}`).start()
115-
const result = await handleApiCall(socketSdk.getReport(reportId), spinner, 'fetching report')
117+
const spinner = ora(`Fetching report with ID ${reportId} (this could take a while)`).start()
118+
/** @type {import('@socketsecurity/sdk').SocketSdkResultType<'getReport'> | undefined} */
119+
let result
120+
for (let retry = 1; !result; ++retry) {
121+
try {
122+
result = await handleApiCall(socketSdk.getReport(reportId), 'fetching report')
123+
} catch (err) {
124+
if (
125+
retry >= MAX_TIMEOUT_RETRY ||
126+
!(err instanceof ErrorWithCause) ||
127+
err.cause?.cause?.response?.statusCode !== 524
128+
) {
129+
throw err
130+
}
131+
}
132+
}
116133

117134
if (result.success === false) {
118135
return handleUnsuccessfulApiResponse('getReport', result, spinner)

lib/utils/api-helpers.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,16 @@ export function handleUnsuccessfulApiResponse (_name, result, spinner) {
2525
/**
2626
* @template T
2727
* @param {Promise<T>} value
28-
* @param {import('ora').Ora} spinner
2928
* @param {string} description
3029
* @returns {Promise<T>}
3130
*/
32-
export async function handleApiCall (value, spinner, description) {
31+
export async function handleApiCall (value, description) {
3332
/** @type {T} */
3433
let result
3534

3635
try {
3736
result = await value
3837
} catch (cause) {
39-
spinner.fail()
4038
throw new ErrorWithCause(`Failed ${description}`, { cause })
4139
}
4240

0 commit comments

Comments
 (0)