Distinguish Spotify playlist restrictions from missing OAuth scopes

This commit is contained in:
2026-11-11 13:41:49 +03:00
parent 0e921617dd
commit a318099add
2 changed files with 20 additions and 2 deletions
+11 -1
View File
@@ -106,7 +106,17 @@ export async function spotifyApi(path, { method = 'GET', body, signal } = {}) {
headers: { Authorization: `Bearer ${token}`, ...(body ? { 'Content-Type': 'application/json' } : {}) },
...(body ? { body: JSON.stringify(body) } : {}),
});
if (response.status === 403) return { error: 'spotify_permission_required', hint: 'Переподключи Spotify в пульте, чтобы разрешить работу с плейлистами.' };
if (response.status === 403) {
const failure = await response.json().catch(() => null);
const reason = `${response.headers.get('www-authenticate') || ''} ${failure?.error?.message || ''}`;
if (/insufficient[_ ](?:client )?scope/i.test(reason)) {
return { error: 'spotify_permission_required', hint: 'Переподключи Spotify в пульте, чтобы разрешить работу с плейлистами.' };
}
if (path.startsWith('playlists/')) {
return { error: 'spotify_playlist_access_denied', hint: 'Spotify разрешает читать и менять состав плейлиста его владельцу и соавторам.' };
}
return { error: 'spotify_access_denied' };
}
if (!response.ok) return { error: 'spotify_request_failed' };
return response.status === 204 ? { ok: true } : response.json();
}