Skip to content

Commit a6cb9c8

Browse files
committed
refactor(api): improve CVE to GHSA error detection and code clarity
- Use case-insensitive error matching for more reliable detection - Add more specific status code patterns (status: 403, status code 403) - Express 30-day duration as readable calculation (30 * 24 * 60 * 60 * 1000) - Remove redundant inline comment - Rename variable for clearer intent (isGitHubRateLimit -> isRateLimitOrNetworkError)
1 parent dae6c0a commit a6cb9c8

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/utils/cve-to-ghsa.mts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ export async function convertCveToGhsa(
1414
const cacheKey = `cve-to-ghsa-${cveId}`
1515
const octokit = getOctokit()
1616

17-
// CVE to GHSA mappings don't change, cache for 30 days (in milliseconds).
18-
const THIRTY_DAYS_MS = 2_592_000_000
17+
const THIRTY_DAYS_MS = 30 * 24 * 60 * 60 * 1000
1918

2019
const response = await cacheFetch(
2120
cacheKey,
@@ -40,16 +39,18 @@ export async function convertCveToGhsa(
4039
}
4140
} catch (e) {
4241
const errorCause = getErrorCause(e)
43-
// Detect GitHub API rate limit errors.
44-
const isGitHubRateLimit =
45-
errorCause.includes('rate limit') ||
46-
errorCause.includes('EPIPE') ||
47-
errorCause.includes('ECONNRESET') ||
48-
errorCause.includes('403')
42+
const errorLower = errorCause.toLowerCase()
43+
// Detect GitHub API rate limit and network errors.
44+
const isRateLimitOrNetworkError =
45+
errorLower.includes('rate limit') ||
46+
errorLower.includes('epipe') ||
47+
errorLower.includes('econnreset') ||
48+
errorLower.includes('status: 403') ||
49+
errorLower.includes('status code 403')
4950

5051
return {
5152
ok: false,
52-
message: isGitHubRateLimit
53+
message: isRateLimitOrNetworkError
5354
? 'GitHub API rate limit exceeded while converting CVE to GHSA. Wait an hour or set SOCKET_CLI_GITHUB_TOKEN environment variable with a personal access token for higher limits.'
5455
: `Failed to convert CVE to GHSA: ${errorCause}`,
5556
}

0 commit comments

Comments
 (0)