Skip to content

Commit fcaf8eb

Browse files
committed
feat(Spotify): throw specific SpotifyResponseError
This allows the actual error message to be displayed
1 parent f929607 commit fcaf8eb

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

providers/Spotify/mod.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,31 @@ export default class SpotifyProvider extends MetadataApiProvider {
6262
}
6363

6464
async query<Data>(apiUrl: URL, maxTimestamp?: number): Promise<CacheEntry<Data>> {
65-
const accessToken = await this.cachedAccessToken(this.requestAccessToken);
66-
const cacheEntry = await this.fetchJSON<Data>(apiUrl, {
67-
policy: { maxTimestamp },
68-
requestInit: {
69-
headers: {
70-
'Authorization': `Bearer ${accessToken}`,
65+
try {
66+
const accessToken = await this.cachedAccessToken(this.requestAccessToken);
67+
const cacheEntry = await this.fetchJSON<Data>(apiUrl, {
68+
policy: { maxTimestamp },
69+
requestInit: {
70+
headers: {
71+
'Authorization': `Bearer ${accessToken}`,
72+
},
7173
},
72-
},
73-
});
74-
const { error } = cacheEntry.content as { error?: ApiError };
75-
76-
if (error) {
77-
throw new SpotifyResponseError(error, apiUrl);
74+
});
75+
const apiError = cacheEntry.content as ApiError;
76+
if (apiError.error) {
77+
throw new SpotifyResponseError(apiError, apiUrl);
78+
}
79+
return cacheEntry;
80+
} catch (error) {
81+
// Clone the response so the body of the original response can be
82+
// consumed later if the error gets re-thrown.
83+
const apiError = await error?.response?.clone().json() as ApiError;
84+
if (apiError?.error) {
85+
throw new SpotifyResponseError(apiError, apiUrl);
86+
} else {
87+
throw error;
88+
}
7889
}
79-
return cacheEntry;
8090
}
8191

8292
private async requestAccessToken(): Promise<ApiAccessToken> {

0 commit comments

Comments
 (0)