mirror of
https://github.com/markuryy/shark.git
synced 2025-12-13 03:51:02 +00:00
fix(dl): add retry logic for network errors and rate limits
This commit is contained in:
@@ -139,8 +139,16 @@ export class DeezerAPI {
|
||||
|
||||
if (hasError) {
|
||||
const errorStr = JSON.stringify(resultJson.error);
|
||||
const errorObj = resultJson.error as any;
|
||||
console.error(`[ERROR] API returned error for ${method}:`, errorStr);
|
||||
|
||||
// Handle rate limiting (error codes 4 and 700) - wait 5 seconds and retry
|
||||
if (errorObj.code && [4, 700].includes(errorObj.code)) {
|
||||
console.log(`[DEBUG] Rate limited (code ${errorObj.code}), waiting 5s before retry...`);
|
||||
await new Promise(resolve => setTimeout(resolve, 5000));
|
||||
return this.apiCall(method, args, params, retryCount);
|
||||
}
|
||||
|
||||
// Handle invalid token - retry with new token (max 2 retries)
|
||||
if ((errorStr.includes('invalid api token') || errorStr.includes('Invalid CSRF token')) && retryCount < 2) {
|
||||
console.log(`[DEBUG] Invalid token, fetching new token... (retry ${retryCount + 1}/2)`);
|
||||
@@ -167,8 +175,17 @@ export class DeezerAPI {
|
||||
|
||||
console.log(`[DEBUG] Returning results for ${method}`);
|
||||
return resultJson.results;
|
||||
} catch (error) {
|
||||
} catch (error: any) {
|
||||
console.error('[ERROR] deezer.gw', method, args, error);
|
||||
|
||||
// Retry on network errors (connection issues, timeouts, etc.)
|
||||
const networkErrors = ['ECONNABORTED', 'ECONNREFUSED', 'ECONNRESET', 'ENETRESET', 'ETIMEDOUT'];
|
||||
if (error.code && networkErrors.includes(error.code)) {
|
||||
console.log(`[DEBUG] Network error (${error.code}), waiting 2s before retry...`);
|
||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||
return this.apiCall(method, args, params, retryCount);
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -272,7 +289,7 @@ export class DeezerAPI {
|
||||
}
|
||||
|
||||
// Get track download URL
|
||||
async getTrackDownloadUrl(trackToken: string, format: string, licenseToken: string): Promise<string | null> {
|
||||
async getTrackDownloadUrl(trackToken: string, format: string, licenseToken: string, retryCount: number = 0): Promise<string | null> {
|
||||
console.log('[DEBUG] Getting track download URL...', { trackToken, format, licenseToken });
|
||||
|
||||
try {
|
||||
@@ -316,9 +333,18 @@ export class DeezerAPI {
|
||||
|
||||
console.error('[ERROR] No download URL in response:', result);
|
||||
return null;
|
||||
} catch (error) {
|
||||
} catch (error: any) {
|
||||
console.error('[ERROR] Failed to get track download URL:', error);
|
||||
throw error; // Re-throw to let caller handle it
|
||||
|
||||
// Retry on network errors
|
||||
const networkErrors = ['ECONNABORTED', 'ECONNREFUSED', 'ECONNRESET', 'ENETRESET', 'ETIMEDOUT'];
|
||||
if (error.code && networkErrors.includes(error.code)) {
|
||||
console.log(`[DEBUG] Network error (${error.code}) getting download URL, waiting 2s before retry...`);
|
||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||
return this.getTrackDownloadUrl(trackToken, format, licenseToken, retryCount);
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user