From ed53810fb33f70722361c67d176ff4edf531ba45 Mon Sep 17 00:00:00 2001 From: TheCatLady <52870424+TheCatLady@users.noreply.github.com> Date: Sat, 4 Dec 2021 12:42:15 -0500 Subject: [PATCH] fix: handle Plex library settings migration failure gracefully (#2254) * fix: handle Plex library settings migration failure gracefully * fix: handle failure in syncLibraries() instead --- server/api/plexapi.ts | 53 +++++++++++++++++++++++++++---------------- server/index.ts | 7 +++--- 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/server/api/plexapi.ts b/server/api/plexapi.ts index cee4a9cd1c..73278387a8 100644 --- a/server/api/plexapi.ts +++ b/server/api/plexapi.ts @@ -1,5 +1,6 @@ import NodePlexAPI from 'plex-api'; import { getSettings, Library, PlexSettings } from '../lib/settings'; +import logger from '../logger'; export interface PlexLibraryItem { ratingKey: string; @@ -145,28 +146,40 @@ class PlexAPI { public async syncLibraries(): Promise { const settings = getSettings(); - const libraries = await this.getLibraries(); - - const newLibraries: Library[] = libraries - // Remove libraries that are not movie or show - .filter((library) => library.type === 'movie' || library.type === 'show') - // Remove libraries that do not have a metadata agent set (usually personal video libraries) - .filter((library) => library.agent !== 'com.plexapp.agents.none') - .map((library) => { - const existing = settings.plex.libraries.find( - (l) => l.id === library.key && l.name === library.title - ); - - return { - id: library.key, - name: library.title, - enabled: existing?.enabled ?? false, - type: library.type, - lastScan: existing?.lastScan, - }; + try { + const libraries = await this.getLibraries(); + + const newLibraries: Library[] = libraries + // Remove libraries that are not movie or show + .filter( + (library) => library.type === 'movie' || library.type === 'show' + ) + // Remove libraries that do not have a metadata agent set (usually personal video libraries) + .filter((library) => library.agent !== 'com.plexapp.agents.none') + .map((library) => { + const existing = settings.plex.libraries.find( + (l) => l.id === library.key && l.name === library.title + ); + + return { + id: library.key, + name: library.title, + enabled: existing?.enabled ?? false, + type: library.type, + lastScan: existing?.lastScan, + }; + }); + + settings.plex.libraries = newLibraries; + } catch (e) { + logger.error('Failed to fetch Plex libraries', { + label: 'Plex API', + message: e.message, }); - settings.plex.libraries = newLibraries; + settings.plex.libraries = []; + } + settings.save(); } diff --git a/server/index.ts b/server/index.ts index f85b027522..9baf22028d 100644 --- a/server/index.ts +++ b/server/index.ts @@ -63,11 +63,12 @@ app }); if (admin) { - const plexapi = new PlexAPI({ plexToken: admin.plexToken }); - await plexapi.syncLibraries(); - logger.info('Migrating libraries to include media type', { + logger.info('Migrating Plex libraries to include media type', { label: 'Settings', }); + + const plexapi = new PlexAPI({ plexToken: admin.plexToken }); + await plexapi.syncLibraries(); } }