-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathgithub-errors.mts
More file actions
345 lines (313 loc) · 11.1 KB
/
Copy pathgithub-errors.mts
File metadata and controls
345 lines (313 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**
* GitHub API error handling for Socket CLI. Converts GitHub REST/GraphQL
* errors into user-friendly CResult failures with actionable messages, and
* hands transient failures to the fleet's shared `pRetry` for backoff.
*/
import { GraphqlResponseError } from '@octokit/graphql'
import { RequestError } from '@octokit/request-error'
import { debugDirNs, debugNs } from '@socketsecurity/lib-stable/debug/output'
import { errorMessage } from '@socketsecurity/lib-stable/errors/message'
import { isError } from '@socketsecurity/lib-stable/errors/predicates'
import { pRetry } from '@socketsecurity/lib-stable/promises/retry'
import { resolveBaseDelayMs } from '@socketsecurity/lib-stable/releases/github-retry-config'
import { formatErrorWithDetail } from '../error/errors.mts'
import type { CResult } from '../../types.mts'
// Upper bound on any single backoff wait. Matches the shared
// GITHUB_RETRY_CONFIG policy in @socketsecurity/lib.
const MAX_RETRY_DELAY_MS = 10_000
// How much each successive backoff wait grows.
const RETRY_BACKOFF_FACTOR = 2
// Canonical `message` values returned by `handleGitHubApiError` /
// `handleGraphqlError`. Exported so callers can short-circuit on
// blocking conditions without matching free-form strings.
export const GITHUB_ERR_ABUSE_DETECTION = 'GitHub abuse detection triggered'
export const GITHUB_ERR_AUTH_FAILED = 'GitHub authentication failed'
export const GITHUB_ERR_GRAPHQL_RATE_LIMIT =
'GitHub GraphQL rate limit exceeded'
export const GITHUB_ERR_RATE_LIMIT = 'GitHub rate limit exceeded'
export function getGitHubRetryWaitTime(e: RequestError): number | undefined {
const retryAfter = e.response?.headers?.['retry-after']
const resetHeader = e.response?.headers?.['x-ratelimit-reset']
let waitTime: number | undefined
if (retryAfter) {
waitTime = Number.parseInt(String(retryAfter), 10)
if (Number.isNaN(waitTime) || waitTime < 0) {
waitTime = undefined
}
} else if (resetHeader) {
const resetTimestamp = Number.parseInt(resetHeader, 10)
if (!Number.isNaN(resetTimestamp)) {
waitTime = Math.max(0, resetTimestamp - Math.floor(Date.now() / 1000))
}
}
return waitTime
}
/**
* Convert GitHub API errors to user-friendly CResult failures. Handles rate
* limits, authentication, and network errors with actionable messages.
*/
export function handleGitHubApiError(
e: unknown,
context: string,
): CResult<never> {
debugNs('error', formatErrorWithDetail(`GitHub API error: ${context}`, e))
debugDirNs('error', e)
if (e instanceof RequestError) {
return handleGitHubRequestError(e, context)
}
// Network errors (ECONNREFUSED, ETIMEDOUT, etc.).
if (isError(e)) {
const code = (e as NodeJS.ErrnoException).code
if (
code === 'ECONNREFUSED' ||
code === 'ENOTFOUND' ||
code === 'ETIMEDOUT'
) {
return {
ok: false,
message: 'Network error connecting to GitHub',
cause:
`Network error while ${context}: ${e.message}\n\n` +
'To resolve:\n' +
'- Check your internet connection\n' +
'- Verify GitHub API is accessible from your network\n' +
'- Check if a proxy or firewall is blocking the connection',
}
}
}
// Generic fallback.
return {
ok: false,
message: 'GitHub API error',
cause: `Unexpected error while ${context}: ${errorMessage(e)}`,
}
}
export function handleGitHubRequestError(
e: RequestError,
context: string,
): CResult<never> {
const { status } = e
// Abuse detection rate limit - check first since it's more specific than standard rate limit.
if (status === 403 && e.message.includes('secondary rate limit')) {
return {
ok: false,
message: GITHUB_ERR_ABUSE_DETECTION,
cause:
`GitHub abuse detection triggered while ${context}. ` +
'This happens when making too many requests in a short period. ' +
'Wait a few minutes before retrying.\n\n' +
'To avoid this:\n' +
'- Reduce the number of concurrent operations\n' +
'- Add delays between bulk operations',
}
}
// Standard rate limit errors (403 with rate limit message or 429).
if (status === 429 || (status === 403 && e.message.includes('rate limit'))) {
const waitTime = getGitHubRetryWaitTime(e)
return {
ok: false,
message: GITHUB_ERR_RATE_LIMIT,
cause:
`GitHub API rate limit exceeded while ${context}. ` +
(waitTime
? `Try again in ${waitTime} seconds.`
: 'Try again in a few minutes.') +
'\n\n' +
'To increase your rate limit:\n' +
'- Set GITHUB_TOKEN environment variable with a valid token\n' +
'- In GitHub Actions, GITHUB_TOKEN is automatically available\n' +
'- Personal access tokens provide higher rate limits than unauthenticated requests',
}
}
// Authentication errors.
if (status === 401) {
return {
ok: false,
message: GITHUB_ERR_AUTH_FAILED,
cause:
`GitHub authentication failed while ${context}. ` +
'Your token may be invalid, expired, or missing required permissions.\n\n' +
'To resolve:\n' +
'- Verify your GitHub token is valid and not expired\n' +
'- Set GITHUB_TOKEN environment variable\n' +
'- Ensure the token has required scopes (repo, read:org)',
}
}
// Permission denied, valid token but insufficient permissions.
if (status === 403 && !e.message.includes('rate limit')) {
return {
ok: false,
message: 'GitHub permission denied',
cause:
`GitHub permission denied while ${context}. ` +
'Your token does not have access to this resource.\n\n' +
'Ensure your token has the required scopes:\n' +
'- repo: Full control of private repositories\n' +
'- read:org: Read org membership (for org repos)',
}
}
// Not found errors.
if (status === 404) {
return {
ok: false,
message: 'GitHub resource not found',
cause:
`GitHub resource not found while ${context}. ` +
'The repository, branch, or file may not exist, or you may not have access to it.\n\n' +
'Verify:\n' +
'- The repository name and owner are correct\n' +
'- The branch exists\n' +
'- Your token has access to the repository',
}
}
// Server errors (5xx).
if (status >= 500) {
return {
ok: false,
message: 'GitHub server error',
cause:
`GitHub server error (${status}) while ${context}. ` +
'GitHub may be experiencing issues.\n\n' +
'To resolve:\n' +
'- Check https://www.githubstatus.com for service status\n' +
'- Try again in a few moments',
}
}
// Other request errors.
return {
ok: false,
message: `GitHub API error (${status})`,
cause: `GitHub API error while ${context}: ${e.message}`,
}
}
/**
* Convert GraphQL errors to user-friendly CResult failures. Handles rate limits
* and authentication errors with actionable messages.
*/
export function handleGraphqlError(
e: unknown,
context: string,
): CResult<never> {
debugNs('error', formatErrorWithDetail(`GraphQL error: ${context}`, e))
debugDirNs('error', e)
if (e instanceof GraphqlResponseError) {
const errorMessages = Array.isArray(e.errors)
? e.errors.map(err => err.message).filter(Boolean)
: []
// Check for rate limit errors.
if (isGraphqlRateLimitError(e)) {
return {
ok: false,
message: GITHUB_ERR_GRAPHQL_RATE_LIMIT,
cause:
`GitHub GraphQL rate limit exceeded while ${context}. ` +
'Try again in a few minutes.\n\n' +
'To increase your rate limit:\n' +
'- Set GITHUB_TOKEN environment variable with a valid token\n' +
'- In GitHub Actions, GITHUB_TOKEN is automatically available',
}
}
// Return the GraphQL error details.
return {
ok: false,
message: 'GitHub GraphQL error',
cause:
`GitHub GraphQL error while ${context}` +
(errorMessages.length ? `:\n- ${errorMessages.join('\n- ')}` : ''),
}
}
// Fall back to REST error handler for non-GraphQL errors.
return handleGitHubApiError(e, context)
}
/**
* Check if a GraphQL error is a rate limit error.
*/
export function isGraphqlRateLimitError(e: unknown): boolean {
if (e instanceof GraphqlResponseError && Array.isArray(e.errors)) {
return e.errors.some(
err =>
err.type === 'RATE_LIMITED' ||
err.message?.toLowerCase().includes('rate limit'),
)
}
return false
}
/**
* Whether another attempt could change the answer. A client error (4xx),
* rate limits included, is already settled — only 5xx and network-level
* failures are worth retrying.
*/
export function isRetryableGitHubError(e: unknown): boolean {
if (e instanceof RequestError) {
const { status } = e
return status < 400 || status >= 500
}
return true
}
/**
* Execute a GitHub API call, retrying transient failures through the fleet's
* shared `pRetry`.
*
* `maxRetries` counts total attempts, so `pRetry` gets one fewer retry on top
* of its initial one. The delay doubles from the base in
* `SOCKET_GITHUB_RETRY_BASE_DELAY_MS` (default 5s, the shared
* GITHUB_RETRY_CONFIG value) and is capped at 10s. That base is read live on
* every call, so a test or a CI job can set it to 0 and skip the real
* wallclock wait — `pRetry` sleeps through `node:timers/promises`, which
* `vi.useFakeTimers()` does not reliably intercept.
*
* Client errors stop the loop early rather than burning its budget, and the
* caller always sees the failure that actually ended the run.
*/
export async function withGitHubRetry<T>(
operation: () => Promise<T>,
context: string,
maxRetries = 3,
): Promise<CResult<T>> {
let attempt = 0
let lastError: unknown
// Boxed so a resolved `undefined` is still recognizable as a success.
let outcome: { data: T } | undefined
try {
await pRetry(
async () => {
attempt += 1
try {
outcome = { data: await operation() }
} catch (e) {
lastError = e
debugNs(
'notice',
`GitHub API attempt ${attempt}/${maxRetries} failed for ${context}`,
)
debugDirNs('error', e)
throw e
}
},
{
backoffFactor: RETRY_BACKOFF_FACTOR,
baseDelayMs: resolveBaseDelayMs(),
maxDelayMs: MAX_RETRY_DELAY_MS,
onRetry(attemptNumber: number, e: unknown, delay: number) {
if (!isRetryableGitHubError(e)) {
// Stop now; another attempt cannot change the answer.
return false
}
debugNs(
'notice',
`Retrying ${context} in ${delay}ms (attempt ${attemptNumber})…`,
)
return undefined
},
onRetryCancelOnFalse: true,
retries: Math.max(0, maxRetries - 1),
},
)
} catch {
// `pRetry` rethrows the failure that ended the run; `lastError` already
// holds it, and it carries the classification handleGitHubApiError needs.
}
return outcome
? { ok: true, data: outcome.data }
: handleGitHubApiError(lastError, context)
}