Skip to content

Commit 95a3fd3

Browse files
committed
Do not crash when caught exception on saving settings
1 parent 043fe76 commit 95a3fd3

File tree

3 files changed

+85
-3
lines changed

3 files changed

+85
-3
lines changed

Flow.Launcher.Core/Plugin/JsonRPCPluginSettings.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public class JsonRPCPluginSettings
2323
protected ConcurrentDictionary<string, object?> Settings { get; set; } = null!;
2424
public required IPublicAPI API { get; init; }
2525

26+
private static readonly string ClassName = nameof(JsonRPCPluginSettings);
27+
2628
private JsonStorage<ConcurrentDictionary<string, object?>> _storage = null!;
2729

2830
private static readonly Thickness SettingPanelMargin = (Thickness)Application.Current.FindResource("SettingPanelMargin");
@@ -122,12 +124,26 @@ public void UpdateSettings(IReadOnlyDictionary<string, object> settings)
122124

123125
public async Task SaveAsync()
124126
{
125-
await _storage.SaveAsync();
127+
try
128+
{
129+
await _storage.SaveAsync();
130+
}
131+
catch (System.Exception e)
132+
{
133+
API.LogException(ClassName, $"Failed to save plugin settings to path: {SettingPath}", e);
134+
}
126135
}
127136

128137
public void Save()
129138
{
130-
_storage.Save();
139+
try
140+
{
141+
_storage.Save();
142+
}
143+
catch (System.Exception e)
144+
{
145+
API.LogException(ClassName, $"Failed to save plugin settings to path: {SettingPath}", e);
146+
}
131147
}
132148

133149
public bool NeedCreateSettingPanel()
Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
using System.IO;
2+
using System.Threading.Tasks;
3+
using CommunityToolkit.Mvvm.DependencyInjection;
24
using Flow.Launcher.Infrastructure.UserSettings;
5+
using Flow.Launcher.Plugin;
36

47
namespace Flow.Launcher.Infrastructure.Storage
58
{
69
public class FlowLauncherJsonStorage<T> : JsonStorage<T> where T : new()
710
{
11+
private static readonly string ClassName = "FlowLauncherJsonStorage";
12+
13+
// We should not initialize API in static constructor because it will create another API instance
14+
private static IPublicAPI api = null;
15+
private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService<IPublicAPI>();
16+
817
public FlowLauncherJsonStorage()
918
{
1019
var directoryPath = Path.Combine(DataLocation.DataDirectory(), DirectoryName);
@@ -13,5 +22,29 @@ public FlowLauncherJsonStorage()
1322
var filename = typeof(T).Name;
1423
FilePath = Path.Combine(directoryPath, $"{filename}{FileSuffix}");
1524
}
25+
26+
public new void Save()
27+
{
28+
try
29+
{
30+
base.Save();
31+
}
32+
catch (System.Exception e)
33+
{
34+
API.LogException(ClassName, $"Failed to save FL settings to path: {FilePath}", e);
35+
}
36+
}
37+
38+
public new async Task SaveAsync()
39+
{
40+
try
41+
{
42+
await base.SaveAsync();
43+
}
44+
catch (System.Exception e)
45+
{
46+
API.LogException(ClassName, $"Failed to save FL settings to path: {FilePath}", e);
47+
}
48+
}
1649
}
17-
}
50+
}

Flow.Launcher.Infrastructure/Storage/PluginJsonStorage.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using System.IO;
2+
using System.Threading.Tasks;
3+
using CommunityToolkit.Mvvm.DependencyInjection;
24
using Flow.Launcher.Infrastructure.UserSettings;
5+
using Flow.Launcher.Plugin;
36

47
namespace Flow.Launcher.Infrastructure.Storage
58
{
@@ -8,6 +11,12 @@ namespace Flow.Launcher.Infrastructure.Storage
811
// Use assembly name to check which plugin is using this storage
912
public readonly string AssemblyName;
1013

14+
private static readonly string ClassName = "PluginJsonStorage";
15+
16+
// We should not initialize API in static constructor because it will create another API instance
17+
private static IPublicAPI api = null;
18+
private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService<IPublicAPI>();
19+
1120
public PluginJsonStorage()
1221
{
1322
// C# related, add python related below
@@ -23,5 +32,29 @@ public PluginJsonStorage(T data) : this()
2332
{
2433
Data = data;
2534
}
35+
36+
public new void Save()
37+
{
38+
try
39+
{
40+
base.Save();
41+
}
42+
catch (System.Exception e)
43+
{
44+
API.LogException(ClassName, $"Failed to save plugin settings to path: {FilePath}", e);
45+
}
46+
}
47+
48+
public new async Task SaveAsync()
49+
{
50+
try
51+
{
52+
await base.SaveAsync();
53+
}
54+
catch (System.Exception e)
55+
{
56+
API.LogException(ClassName, $"Failed to save plugin settings to path: {FilePath}", e);
57+
}
58+
}
2659
}
2760
}

0 commit comments

Comments
 (0)