improve error handling

This commit is contained in:
2025-07-16 13:20:42 +02:00
parent 67249a10bb
commit 09efe80ee6
2 changed files with 63 additions and 9 deletions

View File

@ -78,7 +78,18 @@ export abstract class OauthPlaylistAdder {
this.logger.debug('received access token', resp); this.logger.debug('received access token', resp);
let expiration = new Date(); let expiration = new Date();
expiration.setTime(expiration.getTime() + resp.expires_in * 1000); 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; resp.expires = expiration;
await fs.writeFile(this.token_file_name, JSON.stringify(resp)); await fs.writeFile(this.token_file_name, JSON.stringify(resp));
} }
@ -197,8 +208,19 @@ export abstract class OauthPlaylistAdder {
} }
let expiration = new Date(); let expiration = new Date();
expiration.setTime(expiration.getTime() + resp.expires_in * 1000); 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; 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)); await fs.writeFile(this.token_file_name, JSON.stringify(resp));
return { return {
resp: resp, resp: resp,

View File

@ -113,9 +113,18 @@ export class YoutubePlaylistAdder extends OauthPlaylistAdder implements Playlist
albumItemsUrl.searchParams.append('maxResults', '50'); albumItemsUrl.searchParams.append('maxResults', '50');
albumItemsUrl.searchParams.append('playlistId', albumPlaylistId); albumItemsUrl.searchParams.append('playlistId', albumPlaylistId);
albumItemsUrl.searchParams.append('part', 'snippet'); albumItemsUrl.searchParams.append('part', 'snippet');
const albumPlaylistItem = await fetch(albumItemsUrl, { const albumPlaylistItemRequest = new Request(albumItemsUrl, {
headers: { Authorization: `${token.token_type} ${token.access_token}` } 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 ?? []; const albumTracks: any[] = albumPlaylistItem.items ?? [];
videoIds = albumTracks.map((x) => x.snippet?.resourceId?.videoId).filter((x) => x); videoIds = albumTracks.map((x) => x.snippet?.resourceId?.videoId).filter((x) => x);
this.logger.info( this.logger.info(
@ -205,25 +214,48 @@ export class YoutubePlaylistAdder extends OauthPlaylistAdder implements Playlist
'Add to playlist failed', 'Add to playlist failed',
respObj.error.errors, respObj.error.errors,
respObj.error.code, respObj.error.code,
respObj.error.message respObj.error.message,
'request',
request
); );
throw new Error(respObj.error.errors[0].reason); 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'); const playlistItemsUrl = new URL(this.apiBase + '/playlistItems');
playlistItemsUrl.searchParams.append('videoId', videoId); playlistItemsUrl.searchParams.append('videoId', videoId);
playlistItemsUrl.searchParams.append('playlistId', YOUTUBE_PLAYLIST_ID); playlistItemsUrl.searchParams.append('playlistId', YOUTUBE_PLAYLIST_ID);
playlistItemsUrl.searchParams.append('part', '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}` } 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) { if (existingPlaylistItem.error) {
this.logger.error( this.logger.error(
'Could not check if item is already in playlist', '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; return existingPlaylistItem.pageInfo && existingPlaylistItem.pageInfo.totalResults > 0;
} }