Skip to content

Commit 8451085

Browse files
committed
Improve exception handler
1 parent 35ea1e3 commit 8451085

File tree

2 files changed

+62
-19
lines changed

2 files changed

+62
-19
lines changed

Flow.Launcher/Languages/en.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@
327327
<system:String x:Key="clearlogfolderMessage">Are you sure you want to delete all logs?</system:String>
328328
<system:String x:Key="clearcachefolder">Clear Caches</system:String>
329329
<system:String x:Key="clearcachefolderMessage">Are you sure you want to delete all caches?</system:String>
330-
<system:String x:Key="clearfolderfailMessage">Failed to clear folders and files</system:String>
330+
<system:String x:Key="clearfolderfailMessage">Failed to clear part of folders and files. Please see log file for more information</system:String>
331331
<system:String x:Key="welcomewindow">Wizard</system:String>
332332
<system:String x:Key="userdatapath">User Data Location</system:String>
333333
<system:String x:Key="userdatapathToolTip">User settings and installed plugins are saved in the user data folder. This location may vary depending on whether it's in portable mode or not.</system:String>

Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ namespace Flow.Launcher.SettingPages.ViewModels;
1515

1616
public partial class SettingsPaneAboutViewModel : BaseModel
1717
{
18+
private static readonly string ClassName = nameof(SettingsPaneAboutViewModel);
19+
1820
private readonly Settings _settings;
1921
private readonly Updater _updater;
2022

@@ -103,13 +105,8 @@ private void AskClearLogFolderConfirmation()
103105

104106
if (confirmResult == MessageBoxResult.Yes)
105107
{
106-
try
108+
if (!ClearLogFolder())
107109
{
108-
ClearLogFolder();
109-
}
110-
catch (Exception e)
111-
{
112-
App.API.LogException(nameof(SettingsPaneAboutViewModel), "Failed to clear log folder", e);
113110
App.API.ShowMsgBox(App.API.GetTranslation("clearfolderfailMessage"));
114111
}
115112
}
@@ -126,13 +123,8 @@ private void AskClearCacheFolderConfirmation()
126123

127124
if (confirmResult == MessageBoxResult.Yes)
128125
{
129-
try
130-
{
131-
ClearCacheFolder();
132-
}
133-
catch (Exception e)
126+
if (!ClearCacheFolder())
134127
{
135-
App.API.LogException(nameof(SettingsPaneAboutViewModel), "Failed to clear cache folder", e);
136128
App.API.ShowMsgBox(App.API.GetTranslation("clearfolderfailMessage"));
137129
}
138130
}
@@ -161,19 +153,45 @@ private void OpenLogsFolder()
161153
[RelayCommand]
162154
private Task UpdateAppAsync() => _updater.UpdateAppAsync(false);
163155

164-
private void ClearLogFolder()
156+
private bool ClearLogFolder()
165157
{
158+
var success = true;
166159
var logDirectory = GetLogDir();
167160
var logFiles = GetLogFiles();
168161

169-
logFiles.ForEach(f => f.Delete());
162+
logFiles.ForEach(f =>
163+
{
164+
try
165+
{
166+
f.Delete();
167+
}
168+
catch (Exception e)
169+
{
170+
App.API.LogException(ClassName, $"Failed to delete log file: {f.Name}", e);
171+
success = false;
172+
}
173+
});
170174

171175
logDirectory.EnumerateDirectories("*", SearchOption.TopDirectoryOnly)
176+
// Do not clean log files of current version
172177
.Where(dir => !Constant.Version.Equals(dir.Name))
173178
.ToList()
174-
.ForEach(dir => dir.Delete());
179+
.ForEach(dir =>
180+
{
181+
try
182+
{
183+
dir.Delete();
184+
}
185+
catch (Exception e)
186+
{
187+
App.API.LogException(ClassName, $"Failed to delete log directory: {dir.Name}", e);
188+
success = false;
189+
}
190+
});
175191

176192
OnPropertyChanged(nameof(LogFolderSize));
193+
194+
return success;
177195
}
178196

179197
private static DirectoryInfo GetLogDir(string version = "")
@@ -186,18 +204,43 @@ private static List<FileInfo> GetLogFiles(string version = "")
186204
return GetLogDir(version).EnumerateFiles("*", SearchOption.AllDirectories).ToList();
187205
}
188206

189-
private void ClearCacheFolder()
207+
private bool ClearCacheFolder()
190208
{
209+
var success = true;
191210
var cacheDirectory = GetCacheDir();
192211
var cacheFiles = GetCacheFiles();
193212

194-
cacheFiles.ForEach(f => f.Delete());
213+
cacheFiles.ForEach(f =>
214+
{
215+
try
216+
{
217+
f.Delete();
218+
}
219+
catch (Exception e)
220+
{
221+
App.API.LogException(ClassName, $"Failed to delete cache file: {f.Name}", e);
222+
success = false;
223+
}
224+
});
195225

196226
cacheDirectory.EnumerateDirectories("*", SearchOption.TopDirectoryOnly)
197227
.ToList()
198-
.ForEach(dir => dir.Delete(true));
228+
.ForEach(dir =>
229+
{
230+
try
231+
{
232+
dir.Delete(true);
233+
}
234+
catch (Exception e)
235+
{
236+
App.API.LogException(ClassName, $"Failed to delete cache directory: {dir.Name}", e);
237+
success = false;
238+
}
239+
});
199240

200241
OnPropertyChanged(nameof(CacheFolderSize));
242+
243+
return success;
201244
}
202245

203246
private static DirectoryInfo GetCacheDir()

0 commit comments

Comments
 (0)