diff --git a/server/scanner/LibraryScan.js b/server/scanner/LibraryScan.js index 0366addba..3968c95c1 100644 --- a/server/scanner/LibraryScan.js +++ b/server/scanner/LibraryScan.js @@ -29,6 +29,9 @@ class LibraryScan { /** @type {string[]} */ this.seriesRemovedFromBooks = [] + /** @type {string} item label while a library item is being scanned, so a thrown error can name it */ + this.failureItem = null + this.logs = [] } diff --git a/server/scanner/LibraryScanner.js b/server/scanner/LibraryScanner.js index 7ccb17a11..dcc22caec 100644 --- a/server/scanner/LibraryScanner.js +++ b/server/scanner/LibraryScanner.js @@ -116,7 +116,8 @@ class LibraryScanner { } catch (err) { libraryScan.setComplete() - Logger.error(`[LibraryScanner] Library scan ${libraryScan.id} failed after ${libraryScan.elapsedTimestamp} | ${libraryScan.resultStats}.`, err) + const failedItem = libraryScan.failureItem ? ` while scanning ${libraryScan.failureItem}` : '' + Logger.error(`[LibraryScanner] Library scan ${libraryScan.id} failed after ${libraryScan.elapsedTimestamp} | ${libraryScan.resultStats}${failedItem}.`, err) task.data.scanResults = libraryScan.scanResults const taskFailedString = { @@ -169,6 +170,7 @@ class LibraryScanner { const libraryItemIdsMissing = [] let libraryItemsUpdated = [] for (const existingLibraryItem of existingLibraryItems) { + libraryScan.failureItem = scanItemFailureLabel(existingLibraryItem.title, existingLibraryItem.path) // First try to find matching library item with exact file path let libraryItemData = libraryItemDataFound.find((lid) => lid.path === existingLibraryItem.path) if (!libraryItemData) { @@ -221,6 +223,8 @@ class LibraryScanner { } } + libraryScan.failureItem = null + // Emit item updates in chunks of 10 to client if (libraryItemsUpdated.length === 10) { SocketAuthority.libraryItemsEmitter('items_updated', libraryItemsUpdated) @@ -259,12 +263,14 @@ class LibraryScanner { if (libraryItemDataFound.length) { let newLibraryItems = [] for (const libraryItemData of libraryItemDataFound) { + libraryScan.failureItem = scanItemFailureLabel(libraryItemData.mediaMetadata?.title, libraryItemData.path) const newLibraryItem = await LibraryItemScanner.scanNewLibraryItem(libraryItemData, libraryScan.library.settings, libraryScan) if (newLibraryItem) { newLibraryItems.push(newLibraryItem) libraryScan.resultsAdded++ } + libraryScan.failureItem = null // Emit new items in chunks of 10 to client if (newLibraryItems.length === 10) { @@ -597,7 +603,12 @@ class LibraryScanner { } // Scan library item for updates Logger.debug(`[LibraryScanner] Folder update for relative path "${itemDir}" is in library item "${existingLibraryItem.media.title}" with id "${existingLibraryItem.id}" - scan for updates`) - itemGroupingResults[itemDir] = await LibraryItemScanner.scanLibraryItem(existingLibraryItem.id, updatedLibraryItemDetails) + try { + itemGroupingResults[itemDir] = await LibraryItemScanner.scanLibraryItem(existingLibraryItem.id, updatedLibraryItemDetails) + } catch (err) { + Logger.error(`[LibraryScanner] Failed while scanning ${scanItemFailureLabel(existingLibraryItem.media?.title, existingLibraryItem.path)}`) + throw err + } continue } else if (library.settings.audiobooksOnly && !hasAudioFiles(fileUpdateGroup, itemDir)) { Logger.debug(`[LibraryScanner] Folder update for relative path "${itemDir}" has no audio files`) @@ -626,7 +637,13 @@ class LibraryScanner { Logger.debug(`[LibraryScanner] Folder update group must be a new item "${itemDir}" in library "${library.name}"`) const isSingleMediaItem = isSingleMediaFile(fileUpdateGroup, itemDir) - const newLibraryItem = await LibraryItemScanner.scanPotentialNewLibraryItem(fullPath, library, folder, isSingleMediaItem) + let newLibraryItem + try { + newLibraryItem = await LibraryItemScanner.scanPotentialNewLibraryItem(fullPath, library, folder, isSingleMediaItem) + } catch (err) { + Logger.error(`[LibraryScanner] Failed while scanning ${scanItemFailureLabel(null, fullPath)}`) + throw err + } if (newLibraryItem) { SocketAuthority.libraryItemEmitter('item_added', newLibraryItem) } @@ -638,6 +655,16 @@ class LibraryScanner { } module.exports = new LibraryScanner() +/** + * @param {string} title + * @param {string} path + * @returns {string} + */ +function scanItemFailureLabel(title, path) { + if (title && path) return `"${title}" at "${path}"` + return `"${path || title || 'unknown item'}"` +} + function ItemToFileInoMatch(libraryItem1, libraryItem2) { return libraryItem1.isFile && libraryItem2.libraryFiles.some((lf) => lf.ino === libraryItem1.ino) }