diff --git a/app/MindWork AI Studio/Assistants/DocumentAnalysis/DocumentAnalysisAssistant.razor.cs b/app/MindWork AI Studio/Assistants/DocumentAnalysis/DocumentAnalysisAssistant.razor.cs index ac8a8d43..994bafa8 100644 --- a/app/MindWork AI Studio/Assistants/DocumentAnalysis/DocumentAnalysisAssistant.razor.cs +++ b/app/MindWork AI Studio/Assistants/DocumentAnalysis/DocumentAnalysisAssistant.razor.cs @@ -1,5 +1,3 @@ -using System.Text; - using AIStudio.Chat; using AIStudio.Dialogs; using AIStudio.Dialogs.Settings; diff --git a/app/MindWork AI Studio/Components/ReadFileContent.razor.cs b/app/MindWork AI Studio/Components/ReadFileContent.razor.cs index 4c636567..d09bea4b 100644 --- a/app/MindWork AI Studio/Components/ReadFileContent.razor.cs +++ b/app/MindWork AI Studio/Components/ReadFileContent.razor.cs @@ -29,31 +29,49 @@ public partial class ReadFileContent : MSGComponentBase { var selectedFile = await this.RustService.SelectFile(T("Select file to read its content")); if (selectedFile.UserCancelled) + { + this.Logger.LogInformation("User cancelled the file selection"); return; - + } + if(!File.Exists(selectedFile.SelectedFilePath)) + { + this.Logger.LogWarning("Selected file does not exist: '{FilePath}'", selectedFile.SelectedFilePath); return; - + } + var ext = Path.GetExtension(selectedFile.SelectedFilePath).TrimStart('.'); if (Array.Exists(FileTypeFilter.Executables.FilterExtensions, x => x.Equals(ext, StringComparison.OrdinalIgnoreCase))) { + this.Logger.LogWarning("User attempted to load executable file: {FilePath} with extension: {Extension}", selectedFile.SelectedFilePath, ext); await MessageBus.INSTANCE.SendError(new(Icons.Material.Filled.AppBlocking, T("Executables are not allowed"))); return; } - + if (Array.Exists(FileTypeFilter.AllImages.FilterExtensions, x => x.Equals(ext, StringComparison.OrdinalIgnoreCase))) { + this.Logger.LogWarning("User attempted to load image file: {FilePath} with extension: {Extension}", selectedFile.SelectedFilePath, ext); await MessageBus.INSTANCE.SendWarning(new(Icons.Material.Filled.ImageNotSupported, T("Images are not supported yet"))); return; } - + if (Array.Exists(FileTypeFilter.AllVideos.FilterExtensions, x => x.Equals(ext, StringComparison.OrdinalIgnoreCase))) { + this.Logger.LogWarning("User attempted to load video file: {FilePath} with extension: {Extension}", selectedFile.SelectedFilePath, ext); await MessageBus.INSTANCE.SendWarning(new(Icons.Material.Filled.FeaturedVideo, this.T("Videos are not supported yet"))); return; } - - var fileContent = await UserFile.LoadFileData(selectedFile.SelectedFilePath, this.RustService, this.DialogService); - await this.FileContentChanged.InvokeAsync(fileContent); + + try + { + var fileContent = await UserFile.LoadFileData(selectedFile.SelectedFilePath, this.RustService, this.DialogService); + await this.FileContentChanged.InvokeAsync(fileContent); + this.Logger.LogInformation("Successfully loaded file content: {FilePath}", selectedFile.SelectedFilePath); + } + catch (Exception ex) + { + this.Logger.LogError(ex, "Failed to load file content: {FilePath}", selectedFile.SelectedFilePath); + await MessageBus.INSTANCE.SendError(new(Icons.Material.Filled.Error, T("Failed to load file content"))); + } } } \ No newline at end of file diff --git a/app/MindWork AI Studio/Dialogs/DocumentCheckDialog.razor.cs b/app/MindWork AI Studio/Dialogs/DocumentCheckDialog.razor.cs index 93a8bf19..39abd602 100644 --- a/app/MindWork AI Studio/Dialogs/DocumentCheckDialog.razor.cs +++ b/app/MindWork AI Studio/Dialogs/DocumentCheckDialog.razor.cs @@ -33,10 +33,21 @@ public partial class DocumentCheckDialog : MSGComponentBase { if (firstRender && !string.IsNullOrWhiteSpace(this.FilePath)) { - var fileContent = await UserFile.LoadFileData(this.FilePath, this.RustService, this.DialogService); - this.FileContent = fileContent; - this.StateHasChanged(); + try + { + var fileContent = await UserFile.LoadFileData(this.FilePath, this.RustService, this.DialogService); + this.FileContent = fileContent; + this.StateHasChanged(); + } + catch (Exception ex) + { + this.Logger.LogError(ex, "Failed to load file content from '{FilePath}'", this.FilePath); + this.FileContent = string.Empty; + this.StateHasChanged(); + } } + else if (firstRender) + this.Logger.LogWarning("Document check dialog opened without a valid file path"); } private CodeBlockTheme CodeColorPalette => this.SettingsManager.IsDarkMode ? CodeBlockTheme.Dark : CodeBlockTheme.Default; diff --git a/app/MindWork AI Studio/Settings/ManagedConfiguration.Parsing.cs b/app/MindWork AI Studio/Settings/ManagedConfiguration.Parsing.cs index fea31ecc..99b95203 100644 --- a/app/MindWork AI Studio/Settings/ManagedConfiguration.Parsing.cs +++ b/app/MindWork AI Studio/Settings/ManagedConfiguration.Parsing.cs @@ -1,4 +1,3 @@ -using System; using System.Globalization; using System.Linq.Expressions; diff --git a/app/MindWork AI Studio/Tools/UserFile.cs b/app/MindWork AI Studio/Tools/UserFile.cs index e3299b39..14fc0fb4 100644 --- a/app/MindWork AI Studio/Tools/UserFile.cs +++ b/app/MindWork AI Studio/Tools/UserFile.cs @@ -15,7 +15,7 @@ public static class UserFile /// Attempts to load the content of a file at the specified path, ensuring Pandoc is installed and available before proceeding. /// /// The full path to the file to be read. Must not be null or empty. - /// Rustservice used to read file content. + /// Rust service used to read file content. /// Dialogservice used to display the Pandoc installation dialog if needed. public static async Task LoadFileData(string filePath, RustService rustService, IDialogService dialogService) { diff --git a/app/MindWork AI Studio/wwwroot/changelog/v0.9.55.md b/app/MindWork AI Studio/wwwroot/changelog/v0.9.55.md index fb8ab9d2..b3d95d68 100644 --- a/app/MindWork AI Studio/wwwroot/changelog/v0.9.55.md +++ b/app/MindWork AI Studio/wwwroot/changelog/v0.9.55.md @@ -4,4 +4,5 @@ - Improved the document analysis assistant (in preview) by adding descriptions to the different sections. - Improved the document preview dialog for the document analysis assistant (in preview), providing Markdown and plain text views for attached files. - Improved the ID handling for configuration plugins. +- Improved error handling, logging, and code quality. - Fixed a bug in the local data sources info dialog (preview feature) for data directories that could cause the app to crash. The error was caused by a background thread producing data while the frontend attempted to display it. \ No newline at end of file