From e64e28cbe39cf28ee732d2f1d04af37393174a4e Mon Sep 17 00:00:00 2001 From: Evan Fiordeliso Date: Tue, 1 Jul 2025 11:06:45 -0400 Subject: [PATCH] Add error handling for create entry errors --- .../CreateBookFromGoodreadsUrlCommand.ts | 11 ++++- src/commands/SearchGoodreadsCommand.ts | 12 ++++- src/main.ts | 46 +++++++++---------- .../suggesters/TextInputSuggest.svelte | 16 ++----- .../settings/BookTrackerSettingTabView.svelte | 3 +- 5 files changed, 44 insertions(+), 44 deletions(-) diff --git a/src/commands/CreateBookFromGoodreadsUrlCommand.ts b/src/commands/CreateBookFromGoodreadsUrlCommand.ts index d3ee324..145c4f8 100644 --- a/src/commands/CreateBookFromGoodreadsUrlCommand.ts +++ b/src/commands/CreateBookFromGoodreadsUrlCommand.ts @@ -8,7 +8,7 @@ const GOODREADS_URL_PATTERN = /https:\/\/www.goodreads.com\/book\/show\/(\d+)/; export class CreateBookFromGoodreadsUrlCommand extends Command { constructor( private readonly goodreads: Goodreads, - private readonly cb: (book: Book) => void | PromiseLike + private readonly createEntry: (book: Book) => void | PromiseLike ) { super( "create-book-from-goodreads-url", @@ -40,7 +40,14 @@ export class CreateBookFromGoodreadsUrlCommand extends Command { return; } - await this.cb(book); + try { + await this.createEntry(book); + } catch (error) { + console.error("Failed to create book:", error); + new Notice("Failed to create book. Check console for details."); + return; + } + new Notice("Book created from Goodreads URL."); } } diff --git a/src/commands/SearchGoodreadsCommand.ts b/src/commands/SearchGoodreadsCommand.ts index 931f4fc..2166c6c 100644 --- a/src/commands/SearchGoodreadsCommand.ts +++ b/src/commands/SearchGoodreadsCommand.ts @@ -8,7 +8,7 @@ export class SearchGoodreadsCommand extends Command { constructor( private readonly app: App, private readonly goodreads: Goodreads, - private readonly cb: (book: Book) => void + private readonly createEntry: (book: Book) => void | PromiseLike ) { super("search-goodreads", "Search Goodreads"); } @@ -50,6 +50,14 @@ export class SearchGoodreadsCommand extends Command { return; } - this.cb(book); + try { + await this.createEntry(book); + } catch (error) { + console.error("Failed to create book:", error); + new Notice("Failed to create book. Check console for details."); + return; + } + + new Notice("Book created from search result."); } } diff --git a/src/main.ts b/src/main.ts index 2bc28e8..1604317 100644 --- a/src/main.ts +++ b/src/main.ts @@ -140,34 +140,30 @@ export default class BookTrackerPlugin extends Plugin { } async createEntry(book: Book): Promise { - try { - const fileName = this.templater - .renderTemplate(this.settings.fileNameFormat, { - title: book.title, - authors: book.authors.map((a) => a.name).join(", "), - }) - .replace(/[/:*?<>|""]/g, ""); + const fileName = this.templater + .renderTemplate(this.settings.fileNameFormat, { + title: book.title, + authors: book.authors.map((a) => a.name).join(", "), + }) + .replace(/[/:*?<>|""]/g, ""); - const data: Record = { book }; + const data: Record = { book }; - if (this.settings.downloadCovers && book.coverImageUrl) { - const coverImageFile = await this.downloadCoverImage( - book.coverImageUrl, - fileName - ); - data.coverImagePath = coverImageFile.path; - } - - const renderedContent = await this.templater.renderTemplateFile( - this.settings.templateFile, - data + if (this.settings.downloadCovers && book.coverImageUrl) { + const coverImageFile = await this.downloadCoverImage( + book.coverImageUrl, + fileName ); - - const filePath = this.settings.tbrFolder + "/" + fileName + ".md"; - const file = await this.app.vault.create(filePath, renderedContent); - await this.app.workspace.getLeaf().openFile(file); - } catch (error) { - console.error("Failed to create book entry:", error); + data.coverImagePath = coverImageFile.path; } + + const renderedContent = await this.templater.renderTemplateFile( + this.settings.templateFile, + data + ); + + const filePath = this.settings.tbrFolder + "/" + fileName + ".md"; + const file = await this.app.vault.create(filePath, renderedContent); + await this.app.workspace.getLeaf().openFile(file); } } diff --git a/src/ui/components/suggesters/TextInputSuggest.svelte b/src/ui/components/suggesters/TextInputSuggest.svelte index 6ce2317..ca97c8e 100644 --- a/src/ui/components/suggesters/TextInputSuggest.svelte +++ b/src/ui/components/suggesters/TextInputSuggest.svelte @@ -8,14 +8,12 @@