fix(api): continue crawling when access token refresh fails for one user

This commit is contained in:
Julian Tölle 2021-05-20 19:19:24 +02:00
parent 721f979cc8
commit 0c2188c4d4

View file

@ -50,14 +50,20 @@ export class SpotifyService {
playHistory = await this.spotifyApi.getRecentlyPlayedTracks(user.spotify); playHistory = await this.spotifyApi.getRecentlyPlayedTracks(user.spotify);
} catch (err) { } catch (err) {
if (err.response && err.response.status === 401 && retryOnExpiredToken) { if (err.response && err.response.status === 401 && retryOnExpiredToken) {
const accessToken = await this.spotifyAuth.refreshAccessToken( try {
user.spotify const accessToken = await this.spotifyAuth.refreshAccessToken(
); user.spotify
await this.usersService.updateSpotifyConnection(user, { );
...user.spotify, await this.usersService.updateSpotifyConnection(user, {
accessToken, ...user.spotify,
}); accessToken,
await this.crawlListensForUser(user, false); });
await this.crawlListensForUser(user, false);
} catch (errFromAuth) {
this.logger.error(
`Refreshing access token failed for user "${user.id}": ${errFromAuth}`
);
}
} else { } else {
// TODO sent to sentry // TODO sent to sentry
this.logger.error( this.logger.error(
@ -65,7 +71,7 @@ export class SpotifyService {
); );
} }
// Makes no sense to keep processing the (inexistant) data but if we throw // Makes no sense to keep processing the (inexistent) data but if we throw
// the error the fetch loop will not process other users. // the error the fetch loop will not process other users.
return; return;
} }
@ -111,7 +117,7 @@ export class SpotifyService {
* The variable will still be set, in case we want to add the functionality * The variable will still be set, in case we want to add the functionality
* again. * again.
*/ */
this.usersService.updateSpotifyConnection(user, { await this.usersService.updateSpotifyConnection(user, {
...user.spotify, ...user.spotify,
lastRefreshTime: newestPlayTime, lastRefreshTime: newestPlayTime,
}); });