Repository navigation
Expand file tree
/
Copy patherror-handler.js
More file actions
420 lines (381 loc) · 12.2 KB
/
Copy patherror-handler.js
File metadata and controls
420 lines (381 loc) · 12.2 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/**
* Error Handler Module
*
* Provides error catching, recovery strategies, and suggestions for
* metadata agent operations. Helps users understand what went wrong
* and suggests corrective actions.
*
* @module error-handler
*/
import pino from "pino";
/**
* Logger instance for error handling operations
* @type {pino.Logger}
*/
const logger = pino({
name: "metadata-agent:error-handler",
level: process.env.LOG_LEVEL || "info",
});
/**
* Error type classifications
* @type {Object}
*/
const ERROR_TYPES = {
AUTHENTICATION: "authentication",
AUTHORIZATION: "authorization",
RATE_LIMIT: "rate_limit",
NOT_FOUND: "not_found",
VALIDATION: "validation",
CONFLICT: "conflict",
NETWORK: "network",
UNKNOWN: "unknown",
};
/**
* Catch and classify an error, suggesting recovery strategies
*
* Analyzes an error object and returns:
* - Error type classification
* - Human-readable message
* - Suggested recovery actions
* - Whether the error is retriable
*
* @param {Error|Object} error - The error to handle
* @returns {Object} Error classification { type, message, recovery, retriable, code }
*
* @example
* try {
* await api.getIssues();
* } catch (error) {
* const handled = errorHandler.catch(error);
* console.log(`[${handled.type}] ${handled.message}`);
* console.log(`Recovery: ${handled.recovery}`);
* }
*/
export function catchError(error) {
if (!error) {
return {
type: ERROR_TYPES.UNKNOWN,
message: "Unknown error occurred",
recovery: "Check logs for details",
retriable: false,
code: null,
};
}
// Determine error type
let errorType = ERROR_TYPES.UNKNOWN;
let retriable = false;
let recovery = "Try again";
// Check error status code first (for HTTP errors)
if (error.status || error.statusCode) {
const status = error.status || error.statusCode;
if (status === 401) {
errorType = ERROR_TYPES.AUTHENTICATION;
recovery = "Check GITHUB_TOKEN environment variable";
} else if (status === 403) {
// Could be auth or rate limit
if (error.message && error.message.includes("API rate limit")) {
errorType = ERROR_TYPES.RATE_LIMIT;
recovery = "Wait a few minutes before retrying";
retriable = true;
} else {
errorType = ERROR_TYPES.AUTHORIZATION;
recovery = "Check token has necessary scopes (repo, read:org)";
}
} else if (status === 404) {
errorType = ERROR_TYPES.NOT_FOUND;
recovery = "Verify repository and issue numbers are correct";
} else if (status === 422) {
errorType = ERROR_TYPES.VALIDATION;
recovery = "Check label names are valid and issue exists";
} else if (status === 409) {
errorType = ERROR_TYPES.CONFLICT;
recovery = "Issue state changed, refresh and try again";
retriable = true;
} else if (status >= 500) {
recovery = "GitHub API is experiencing issues, try again later";
retriable = true;
}
}
// Check error message patterns
const message = error.message || String(error);
if (message.includes("ETIMEDOUT") || message.includes("ECONNRESET")) {
errorType = ERROR_TYPES.NETWORK;
recovery = "Check internet connection, try again";
retriable = true;
} else if (message.includes("rate limit")) {
errorType = ERROR_TYPES.RATE_LIMIT;
recovery = "Wait before retrying";
retriable = true;
} else if (message.includes("unauthorized") || message.includes("401")) {
errorType = ERROR_TYPES.AUTHENTICATION;
recovery = "Check GITHUB_TOKEN environment variable";
} else if (message.includes("forbidden") || message.includes("403")) {
errorType = ERROR_TYPES.AUTHORIZATION;
recovery = "Check token scopes (repo, read:org)";
} else if (message.includes("not found") || message.includes("404")) {
errorType = ERROR_TYPES.NOT_FOUND;
recovery = "Verify resource exists";
} else if (message.includes("validation") || message.includes("invalid")) {
errorType = ERROR_TYPES.VALIDATION;
recovery = "Check input parameters are valid";
}
logger.warn(
{ type: errorType, code: error.status || error.code, message },
"Error caught and classified",
);
return {
type: errorType,
message: message || "An unexpected error occurred",
recovery,
retriable,
code: error.status || error.code || null,
original: error,
};
}
/**
* Retry a function with exponential backoff and error handling
*
* Automatically retries on retriable errors (rate limits, timeouts, 5xx errors).
* Does not retry on non-retriable errors (auth, validation, 4xx except 429).
*
* @async
* @param {Function} fn - Async function to retry
* @param {Object} options - Retry options
* @param {number} [options.maxAttempts] - Maximum retry attempts (default: 3)
* @param {number} [options.backoffMs] - Initial backoff time in ms (default: 1000)
* @param {number} [options.maxBackoffMs] - Maximum backoff time (default: 60000)
* @param {Function} [options.onRetry] - Callback called before each retry
* @returns {Promise<any>} Result of the function
* @throws {Error} If all retries exhausted or error is non-retriable
*
* @example
* const result = await errorHandler.retry(
* () => api.getIssues({ owner, repo }),
* { maxAttempts: 5, backoffMs: 500 }
* );
*/
export async function retry(fn, options = {}) {
const {
maxAttempts = 3,
backoffMs = 1000,
maxBackoffMs = 60000,
onRetry = null,
} = options;
let lastError;
let lastHandled;
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
return await fn();
} catch (error) {
lastError = error;
lastHandled = catchError(error);
// Don't retry non-retriable errors
if (!lastHandled.retriable) {
logger.error(
{ type: lastHandled.type, attempt, message: lastHandled.message },
"Non-retriable error, not retrying",
);
throw error;
}
// Don't wait after final attempt
if (attempt >= maxAttempts) {
break;
}
// Calculate backoff with exponential growth
const waitMs = Math.min(
backoffMs * Math.pow(2, attempt - 1),
maxBackoffMs,
);
logger.warn(
{ attempt, maxAttempts, waitMs, type: lastHandled.type },
"Retriable error, retrying",
);
// Call retry callback if provided
if (onRetry) {
onRetry({
attempt,
maxAttempts,
waitMs,
error: lastHandled,
});
}
// Wait before retry
await new Promise((resolve) => setTimeout(resolve, waitMs));
}
}
// All retries exhausted
logger.error(
{ maxAttempts, type: lastHandled?.type },
"All retry attempts exhausted",
);
throw new Error(
`Failed after ${maxAttempts} attempts: ${lastHandled?.message || lastError.message}`,
);
}
/**
* Suggest corrective actions for an error
*
* Provides structured suggestions for resolving an error, including:
* - Immediate actions to try
* - Information to check
* - Who to contact if it persists
*
* @param {Error|Object} error - The error to get suggestions for
* @returns {Object} Suggestions { actions, checks, escalation }
*
* @example
* const error = new Error('API rate limit exceeded');
* const suggestions = errorHandler.suggest(error);
* console.log(suggestions.actions[0]); // "Wait 60 seconds..."
*/
export function suggest(error) {
const handled = catchError(error);
const suggestions = {
type: handled.type,
immediate: [],
checks: [],
escalation: null,
};
switch (handled.type) {
case ERROR_TYPES.AUTHENTICATION:
suggestions.immediate = [
"Check GITHUB_TOKEN is set: echo $GITHUB_TOKEN",
"Verify token is not empty or expired",
"Regenerate token if needed at github.com/settings/tokens",
];
suggestions.checks = [
'Token has "repo" scope',
'Token has "read:org" scope',
"Token is not restricted to specific repos",
];
suggestions.escalation = "Contact GitHub Support if token issues persist";
break;
case ERROR_TYPES.AUTHORIZATION:
suggestions.immediate = [
"Check token scopes: repo, read:org, write:discussion",
"Verify account has access to the repository",
"Check if organization requires SAML/SSO",
];
suggestions.checks = [
"GitHub user has collaborator/owner access",
"Organization SAML is not blocking the token",
"Repository is not archived or restricted",
];
suggestions.escalation = "Contact repository admin or GitHub Support";
break;
case ERROR_TYPES.RATE_LIMIT:
suggestions.immediate = [
"Wait a few minutes before retrying",
"Check rate limit: gh api rate_limit",
"Use --delay flag to slow down batch operations",
];
suggestions.checks = [
"Verify not running multiple concurrent agents",
"Check for webhook loops (issues triggering workflows)",
"Consider using GraphQL for batch queries",
];
suggestions.escalation = "Request higher rate limit from GitHub";
break;
case ERROR_TYPES.VALIDATION:
suggestions.immediate = [
"Check label names in error message",
"Verify label exists: gh label list",
"Ensure issue number is correct",
];
suggestions.checks = [
"Label family is correct (e.g., type:, status:)",
"Label is spelled correctly",
"Issue or PR exists and is not deleted",
];
suggestions.escalation = "Create missing labels if needed";
break;
case ERROR_TYPES.NOT_FOUND:
suggestions.immediate = [
"Verify owner and repo: gh repo view <owner>/<repo>",
"Check issue exists: gh issue view <number>",
"Confirm you have access to view the repository",
];
suggestions.checks = [
"Repository name is correct",
"Repository is not private (or you have access)",
"Issue number is correct and not deleted",
];
suggestions.escalation =
"Check with team if repo was transferred/deleted";
break;
case ERROR_TYPES.NETWORK:
suggestions.immediate = [
"Check internet connection",
"Try again in a few moments",
"Check GitHub status: status.github.com",
];
suggestions.checks = [
"Network connectivity is stable",
"No firewall/proxy blocking GitHub.com",
"DNS resolution is working",
];
suggestions.escalation = "Contact network admin if persistent";
break;
default:
suggestions.immediate = [
"Check logs for detailed error message",
"Try the operation again",
"Check GitHub status page",
];
suggestions.checks = [
"All required parameters are provided",
"Input data is valid and complete",
"GitHub API is operational",
];
suggestions.escalation = "Review error logs and GitHub API documentation";
break;
}
logger.info(
{ type: handled.type, actionCount: suggestions.immediate.length },
"Suggestions generated",
);
return suggestions;
}
/**
* Format an error for display to users
*
* Creates a clean, user-friendly error message with context and suggestions.
*
* @param {Error|Object} error - The error to format
* @param {boolean} [includeStack] - Include stack trace (default: false)
* @returns {string} Formatted error message
*
* @example
* console.error(errorHandler.format(error));
* // Output:
* // [AUTHENTICATION ERROR]
* // GitHub authentication failed: Invalid token
* //
* // Recovery: Check GITHUB_TOKEN environment variable
*/
export function format(error, includeStack = false) {
const handled = catchError(error);
const typeLabel = handled.type.toUpperCase().replace(/_/g, " ");
let output = `[${typeLabel} ERROR]\n`;
output += `${handled.message}\n\n`;
output += `Recovery: ${handled.recovery}`;
if (includeStack && handled.original && handled.original.stack) {
output += `\n\nStack trace:\n${handled.original.stack}`;
}
return output;
}
/**
* Error Handler export object
* Provides all error handling functions in a single namespace
*
* @type {Object}
* @exports error-handler
*/
export const errorHandler = {
catch: catchError,
retry,
suggest,
format,
ERROR_TYPES,
};
export default errorHandler;