From 56b7cf4e8e2a58992a0187f96232853726798760 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 13 Jun 2025 13:27:12 +0000 Subject: [PATCH] Fix template loading with proper error handling Co-authored-by: estruyf <2900833+estruyf@users.noreply.github.com> --- src/helpers/ArticleHelper.ts | 13 +++++++++---- src/helpers/ContentType.ts | 17 +++++++++++++++-- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/helpers/ArticleHelper.ts b/src/helpers/ArticleHelper.ts index 9f96db7a..6ebdd462 100644 --- a/src/helpers/ArticleHelper.ts +++ b/src/helpers/ArticleHelper.ts @@ -149,12 +149,17 @@ export class ArticleHelper { * @returns A promise that resolves to the contents of the file, or undefined if the file does not exist. */ public static async getContents(filePath: string): Promise { - const file = await workspace.fs.readFile(Uri.file(parseWinPath(filePath))); - if (!file) { + try { + const file = await workspace.fs.readFile(Uri.file(parseWinPath(filePath))); + if (!file) { + return undefined; + } + + return new TextDecoder().decode(file); + } catch (error) { + Logger.error(`ArticleHelper.getContents: Failed to read file ${filePath}: ${error}`); return undefined; } - - return new TextDecoder().decode(file); } /** diff --git a/src/helpers/ContentType.ts b/src/helpers/ContentType.ts index 14438c26..ab47433a 100644 --- a/src/helpers/ContentType.ts +++ b/src/helpers/ContentType.ts @@ -956,8 +956,21 @@ export class ContentType { let templatePath = contentType.template; let templateData: ParsedFrontMatter | null | undefined = null; if (templatePath) { - templatePath = Folders.getAbsFilePath(templatePath); - templateData = await ArticleHelper.getFrontMatterByPath(templatePath); + try { + templatePath = Folders.getAbsFilePath(templatePath); + templateData = await ArticleHelper.getFrontMatterByPath(templatePath); + if (!templateData) { + Logger.warning(`ContentType.create: Template file not found or could not be parsed: ${templatePath}`); + Notifications.warning( + l10n.t(LocalizationKey.commonError) + ` Template not found: ${templatePath}` + ); + } + } catch (error) { + Logger.error(`ContentType.create: Error loading template from ${templatePath}: ${error}`); + Notifications.error( + l10n.t(LocalizationKey.commonError) + ` Template loading failed: ${templatePath}` + ); + } } const newFilePath: string | undefined = await ArticleHelper.createContent(