From 80559ce199d73d6be0895a305314270325776e46 Mon Sep 17 00:00:00 2001 From: Gadfly Date: Tue, 27 Aug 2024 10:40:49 +0800 Subject: [PATCH] fix: try-catch OpenFolderPickerAsync to avoid crash when select a directory is NOT exist --- src/ViewModels/CommitDetail.cs | 15 +++++++++++---- src/ViewModels/Histories.cs | 17 ++++++++++++----- src/Views/AddWorktree.axaml.cs | 14 +++++++++++--- src/Views/Clone.axaml.cs | 14 +++++++++++--- src/Views/Preference.axaml.cs | 13 ++++++++++--- src/Views/WelcomeToolbar.axaml.cs | 14 +++++++++++--- 6 files changed, 66 insertions(+), 21 deletions(-) diff --git a/src/ViewModels/CommitDetail.cs b/src/ViewModels/CommitDetail.cs index f6ff79b2..faef94ef 100644 --- a/src/ViewModels/CommitDetail.cs +++ b/src/ViewModels/CommitDetail.cs @@ -390,11 +390,18 @@ namespace SourceGit.ViewModels return; var options = new FolderPickerOpenOptions() { AllowMultiple = false }; - var selected = await storageProvider.OpenFolderPickerAsync(options); - if (selected.Count == 1) + try { - var saveTo = Path.Combine(selected[0].Path.LocalPath, Path.GetFileName(file.Path)); - Commands.SaveRevisionFile.Run(_repo.FullPath, _commit.SHA, file.Path, saveTo); + var selected = await storageProvider.OpenFolderPickerAsync(options); + if (selected.Count == 1) + { + var saveTo = Path.Combine(selected[0].Path.LocalPath, Path.GetFileName(file.Path)); + Commands.SaveRevisionFile.Run(_repo.FullPath, _commit.SHA, file.Path, saveTo); + } + } + catch (Exception e) + { + App.RaiseException(_repo.FullPath, $"Failed to save file: {e.Message}"); } ev.Handled = true; diff --git a/src/ViewModels/Histories.cs b/src/ViewModels/Histories.cs index ef3ef953..4ec4e716 100644 --- a/src/ViewModels/Histories.cs +++ b/src/ViewModels/Histories.cs @@ -454,12 +454,19 @@ namespace SourceGit.ViewModels return; var options = new FolderPickerOpenOptions() { AllowMultiple = false }; - var selected = await storageProvider.OpenFolderPickerAsync(options); - if (selected.Count == 1) + try { - var succ = new Commands.FormatPatch(_repo.FullPath, commit.SHA, selected[0].Path.LocalPath).Exec(); - if (succ) - App.SendNotification(_repo.FullPath, App.Text("SaveAsPatchSuccess")); + var selected = await storageProvider.OpenFolderPickerAsync(options); + if (selected.Count == 1) + { + var succ = new Commands.FormatPatch(_repo.FullPath, commit.SHA, selected[0].Path.LocalPath).Exec(); + if (succ) + App.SendNotification(_repo.FullPath, App.Text("SaveAsPatchSuccess")); + } + } + catch (Exception exception) + { + App.RaiseException(_repo.FullPath, $"Failed to save as patch: {exception.Message}"); } e.Handled = true; diff --git a/src/Views/AddWorktree.axaml.cs b/src/Views/AddWorktree.axaml.cs index 0f9d00df..c47dca7d 100644 --- a/src/Views/AddWorktree.axaml.cs +++ b/src/Views/AddWorktree.axaml.cs @@ -1,3 +1,4 @@ +using System; using Avalonia.Controls; using Avalonia.Interactivity; using Avalonia.Platform.Storage; @@ -18,9 +19,16 @@ namespace SourceGit.Views return; var options = new FolderPickerOpenOptions() { AllowMultiple = false }; - var selected = await toplevel.StorageProvider.OpenFolderPickerAsync(options); - if (selected.Count == 1) - TxtLocation.Text = selected[0].Path.LocalPath; + try + { + var selected = await toplevel.StorageProvider.OpenFolderPickerAsync(options); + if (selected.Count == 1) + TxtLocation.Text = selected[0].Path.LocalPath; + } + catch (Exception exception) + { + App.RaiseException(string.Empty, $"Failed to select location: {exception.Message}"); + } e.Handled = true; } diff --git a/src/Views/Clone.axaml.cs b/src/Views/Clone.axaml.cs index a05a68d6..1c299211 100644 --- a/src/Views/Clone.axaml.cs +++ b/src/Views/Clone.axaml.cs @@ -1,3 +1,4 @@ +using System; using Avalonia.Controls; using Avalonia.Interactivity; using Avalonia.Platform.Storage; @@ -18,9 +19,16 @@ namespace SourceGit.Views if (toplevel == null) return; - var selected = await toplevel.StorageProvider.OpenFolderPickerAsync(options); - if (selected.Count == 1) - TxtParentFolder.Text = selected[0].Path.LocalPath; + try + { + var selected = await toplevel.StorageProvider.OpenFolderPickerAsync(options); + if (selected.Count == 1) + TxtParentFolder.Text = selected[0].Path.LocalPath; + } + catch (Exception exception) + { + App.RaiseException(string.Empty, $"Failed to select parent folder: {exception.Message}"); + } e.Handled = true; } diff --git a/src/Views/Preference.axaml.cs b/src/Views/Preference.axaml.cs index 28bdeeaa..20e34209 100644 --- a/src/Views/Preference.axaml.cs +++ b/src/Views/Preference.axaml.cs @@ -186,10 +186,17 @@ namespace SourceGit.Views private async void SelectDefaultCloneDir(object _1, RoutedEventArgs _2) { var options = new FolderPickerOpenOptions() { AllowMultiple = false }; - var selected = await StorageProvider.OpenFolderPickerAsync(options); - if (selected.Count == 1) + try { - ViewModels.Preference.Instance.GitDefaultCloneDir = selected[0].Path.LocalPath; + var selected = await StorageProvider.OpenFolderPickerAsync(options); + if (selected.Count == 1) + { + ViewModels.Preference.Instance.GitDefaultCloneDir = selected[0].Path.LocalPath; + } + } + catch (Exception e) + { + App.RaiseException(string.Empty, $"Failed to select default clone directory: {e.Message}"); } } diff --git a/src/Views/WelcomeToolbar.axaml.cs b/src/Views/WelcomeToolbar.axaml.cs index 6c5e21d0..b38b5b9d 100644 --- a/src/Views/WelcomeToolbar.axaml.cs +++ b/src/Views/WelcomeToolbar.axaml.cs @@ -1,3 +1,4 @@ +using System; using System.IO; using Avalonia.Controls; @@ -30,9 +31,16 @@ namespace SourceGit.Views options.SuggestedStartLocation = folder; } - var selected = await topLevel.StorageProvider.OpenFolderPickerAsync(options); - if (selected.Count == 1) - OpenOrInitRepository(selected[0].Path.LocalPath); + try + { + var selected = await topLevel.StorageProvider.OpenFolderPickerAsync(options); + if (selected.Count == 1) + OpenOrInitRepository(selected[0].Path.LocalPath); + } + catch (Exception exception) + { + App.RaiseException(string.Empty, $"Failed to open repository: {exception.Message}"); + } e.Handled = true; }