Skip to content

Commit 805a917

Browse files
committed
retry on 5xx
1 parent d9e419e commit 805a917

File tree

1 file changed

+73
-12
lines changed

1 file changed

+73
-12
lines changed

assets/auth.js

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ export const Auth = (() => {
241241
};
242242

243243
// API function with auth headers
244-
const githubAPI = async (endpoint, options = {}) => {
244+
const githubAPI = async (endpoint, options = {}, retries = 3) => {
245245
const headers = {
246246
Accept: "application/vnd.github.v3+json",
247247
...options.headers,
@@ -252,16 +252,64 @@ export const Auth = (() => {
252252
headers["Authorization"] = `token ${token}`;
253253
}
254254

255-
const response = await fetch(`${CONFIG.API_BASE}${endpoint}`, {
256-
...options,
257-
headers,
258-
});
255+
let lastError;
256+
for (let attempt = 0; attempt <= retries; attempt++) {
257+
try {
258+
const response = await fetch(`${CONFIG.API_BASE}${endpoint}`, {
259+
...options,
260+
headers,
261+
});
259262

260-
if (!response.ok && response.status === 401) {
261-
handleAuthError();
262-
}
263+
// Handle auth errors
264+
if (!response.ok && response.status === 401) {
265+
handleAuthError();
266+
}
267+
268+
// Log error responses
269+
if (!response.ok) {
270+
console.warn(`GitHub REST API request failed: ${response.status} ${response.statusText}`);
271+
console.warn(`Endpoint: ${endpoint}`);
272+
console.warn('Response headers:', Object.fromEntries(response.headers.entries()));
273+
274+
// Try to read response body if available (may fail due to CORS)
275+
if (response.status >= 500) {
276+
try {
277+
const responseClone = response.clone();
278+
const responseText = await responseClone.text();
279+
console.warn('Response body:', responseText);
280+
} catch (e) {
281+
console.warn('Could not read response body due to CORS restrictions');
282+
}
283+
}
284+
285+
// Retry on all 500+ server errors
286+
if (response.status >= 500 && attempt < retries) {
287+
const delay = Math.min(1000 * Math.pow(2, attempt), 10000); // Exponential backoff, max 10s
288+
console.warn(`Retry attempt ${attempt + 1}/${retries}, waiting ${delay}ms...`);
289+
await new Promise(resolve => setTimeout(resolve, delay));
290+
continue;
291+
}
292+
}
263293

264-
return response;
294+
return response;
295+
} catch (error) {
296+
lastError = error;
297+
console.error('GitHub REST API network error:', error);
298+
299+
// If it's a network error and we have retries left, try again
300+
if (attempt < retries) {
301+
const delay = Math.min(1000 * Math.pow(2, attempt), 10000);
302+
console.warn(`Network error, retrying in ${delay}ms... (attempt ${attempt + 1}/${retries})`);
303+
await new Promise(resolve => setTimeout(resolve, delay));
304+
continue;
305+
}
306+
307+
throw error;
308+
}
309+
}
310+
311+
console.error('GitHub REST API request failed after all retries:', lastError);
312+
throw lastError;
265313
};
266314

267315
// GraphQL API function with retry logic
@@ -306,10 +354,23 @@ export const Auth = (() => {
306354
handleAuthError();
307355
}
308356

309-
// Retry on 502, 503, 504 gateway/server errors
310-
if (response.status >= 502 && response.status <= 504 && attempt < retries) {
357+
// Retry on all 500+ server errors
358+
if (response.status >= 500 && attempt < retries) {
311359
const delay = Math.min(1000 * Math.pow(2, attempt), 10000); // Exponential backoff, max 10s
312-
console.warn(`GraphQL request failed with ${response.status}, retrying in ${delay}ms... (attempt ${attempt + 1}/${retries})`);
360+
361+
// Log all available response information
362+
console.warn(`GraphQL request failed with ${response.status} ${response.statusText}`);
363+
console.warn(`Retry attempt ${attempt + 1}/${retries}, waiting ${delay}ms...`);
364+
console.warn('Response headers:', Object.fromEntries(response.headers.entries()));
365+
366+
// Try to read response body if available (may fail due to CORS)
367+
try {
368+
const responseText = await response.text();
369+
console.warn('Response body:', responseText);
370+
} catch (e) {
371+
console.warn('Could not read response body due to CORS restrictions');
372+
}
373+
313374
await new Promise(resolve => setTimeout(resolve, delay));
314375
continue;
315376
}

0 commit comments

Comments
 (0)