improve error handling
This commit is contained in:
@ -78,7 +78,18 @@ export abstract class OauthPlaylistAdder {
|
||||
this.logger.debug('received access token', resp);
|
||||
let expiration = new Date();
|
||||
expiration.setTime(expiration.getTime() + resp.expires_in * 1000);
|
||||
expiration.setSeconds(expiration.getSeconds() + resp.expires_in);
|
||||
//expiration.setSeconds(expiration.getSeconds() + resp.expires_in);
|
||||
let now = new Date();
|
||||
this.logger.debug(
|
||||
'now is',
|
||||
now,
|
||||
now.getTime(),
|
||||
'token expires in',
|
||||
resp.expires_in,
|
||||
'that would be',
|
||||
expiration,
|
||||
expiration.getTime()
|
||||
);
|
||||
resp.expires = expiration;
|
||||
await fs.writeFile(this.token_file_name, JSON.stringify(resp));
|
||||
}
|
||||
@ -197,8 +208,19 @@ export abstract class OauthPlaylistAdder {
|
||||
}
|
||||
let expiration = new Date();
|
||||
expiration.setTime(expiration.getTime() + resp.expires_in * 1000);
|
||||
expiration.setSeconds(expiration.getSeconds() + resp.expires_in);
|
||||
//expiration.setSeconds(expiration.getSeconds() + resp.expires_in);
|
||||
resp.expires = expiration;
|
||||
let now = new Date();
|
||||
this.logger.debug(
|
||||
'now is',
|
||||
now,
|
||||
now.getTime(),
|
||||
'token expires in',
|
||||
resp.expires_in,
|
||||
'that would be',
|
||||
expiration,
|
||||
expiration.getTime()
|
||||
);
|
||||
await fs.writeFile(this.token_file_name, JSON.stringify(resp));
|
||||
return {
|
||||
resp: resp,
|
||||
|
@ -113,9 +113,18 @@ export class YoutubePlaylistAdder extends OauthPlaylistAdder implements Playlist
|
||||
albumItemsUrl.searchParams.append('maxResults', '50');
|
||||
albumItemsUrl.searchParams.append('playlistId', albumPlaylistId);
|
||||
albumItemsUrl.searchParams.append('part', 'snippet');
|
||||
const albumPlaylistItem = await fetch(albumItemsUrl, {
|
||||
const albumPlaylistItemRequest = new Request(albumItemsUrl, {
|
||||
headers: { Authorization: `${token.token_type} ${token.access_token}` }
|
||||
}).then((r) => r.json());
|
||||
});
|
||||
const albumPlaylistItem = await fetch(albumPlaylistItemRequest).then((r) => r.json());
|
||||
if (albumPlaylistItem.error) {
|
||||
this.logger.info(
|
||||
'Could not check album tracks',
|
||||
albumPlaylistItem.error,
|
||||
'request',
|
||||
albumPlaylistItemRequest
|
||||
);
|
||||
}
|
||||
const albumTracks: any[] = albumPlaylistItem.items ?? [];
|
||||
videoIds = albumTracks.map((x) => x.snippet?.resourceId?.videoId).filter((x) => x);
|
||||
this.logger.info(
|
||||
@ -205,25 +214,48 @@ export class YoutubePlaylistAdder extends OauthPlaylistAdder implements Playlist
|
||||
'Add to playlist failed',
|
||||
respObj.error.errors,
|
||||
respObj.error.code,
|
||||
respObj.error.message
|
||||
respObj.error.message,
|
||||
'request',
|
||||
request
|
||||
);
|
||||
throw new Error(respObj.error.errors[0].reason);
|
||||
}
|
||||
}
|
||||
|
||||
private async isVideoInPlaylist(videoId: string, token: OauthResponse): Promise<boolean> {
|
||||
private async isVideoInPlaylist(
|
||||
videoId: string,
|
||||
token: OauthResponse,
|
||||
retry: boolean = true
|
||||
): Promise<boolean> {
|
||||
const playlistItemsUrl = new URL(this.apiBase + '/playlistItems');
|
||||
playlistItemsUrl.searchParams.append('videoId', videoId);
|
||||
playlistItemsUrl.searchParams.append('playlistId', YOUTUBE_PLAYLIST_ID);
|
||||
playlistItemsUrl.searchParams.append('part', 'id');
|
||||
const existingPlaylistItem: YoutubePlaylistItemResponse = await fetch(playlistItemsUrl, {
|
||||
let existingPlaylistItemRequest = new Request(playlistItemsUrl, {
|
||||
headers: { Authorization: `${token.token_type} ${token.access_token}` }
|
||||
}).then((r) => r.json());
|
||||
});
|
||||
let existingPlaylistItem: YoutubePlaylistItemResponse = await fetch(
|
||||
existingPlaylistItemRequest
|
||||
).then((r) => r.json());
|
||||
if (existingPlaylistItem.error) {
|
||||
this.logger.error(
|
||||
'Could not check if item is already in playlist',
|
||||
existingPlaylistItem.error
|
||||
existingPlaylistItem.error,
|
||||
'request',
|
||||
existingPlaylistItemRequest
|
||||
);
|
||||
|
||||
if (existingPlaylistItem.error.code === 401) {
|
||||
const newToken = await this.refreshToken(true);
|
||||
if (newToken === null) {
|
||||
return false;
|
||||
}
|
||||
if (retry) {
|
||||
return await this.isVideoInPlaylist(videoId, newToken, false);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return existingPlaylistItem.pageInfo && existingPlaylistItem.pageInfo.totalResults > 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user