Skip to content

Commit 85952da

Browse files
CopilotBornToBeRoot
andcommitted
Fix exception handling for LocalSettingsManager and path validation
Co-authored-by: BornToBeRoot <16019165+BornToBeRoot@users.noreply.github.com>
1 parent 3dae961 commit 85952da

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

Source/NETworkManager.Settings/LocalSettingsManager.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,20 @@ public static void Load()
101101
{
102102
Current = JsonSerializer.Deserialize<LocalSettingsInfo>(jsonString, JsonOptions);
103103

104-
Log.Info("Local settings loaded successfully.");
105-
106-
// Reset change tracking
107-
Current.SettingsChanged = false;
108-
109-
return;
104+
// Check if deserialization returned null (e.g., file contains "null")
105+
if (Current == null)
106+
{
107+
Log.Warn("Local settings deserialized to null, initializing new local settings.");
108+
}
109+
else
110+
{
111+
Log.Info("Local settings loaded successfully.");
112+
113+
// Reset change tracking
114+
Current.SettingsChanged = false;
115+
116+
return;
117+
}
110118
}
111119
}
112120
catch (Exception ex)

Source/NETworkManager.Settings/SettingsManager.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ private static string ValidateSettingsFolderPath(string path, string pathSource,
151151
// Validate that the path doesn't contain invalid characters
152152
try
153153
{
154-
// This will throw ArgumentException, NotSupportedException, or SecurityException if the path is invalid
154+
// This will throw ArgumentException, NotSupportedException, SecurityException, PathTooLongException, or IOException if the path is invalid
155155
var fullPath = Path.GetFullPath(path);
156156

157157
// Check if the path is a directory (not a file)
@@ -178,6 +178,16 @@ private static string ValidateSettingsFolderPath(string path, string pathSource,
178178
Log.Error($"Insufficient permissions to access {pathSource} SettingsFolderLocation: {path}. Falling back to {fallbackMessage}.", ex);
179179
return null;
180180
}
181+
catch (PathTooLongException ex)
182+
{
183+
Log.Error($"{pathSource} SettingsFolderLocation path is too long: {path}. Falling back to {fallbackMessage}.", ex);
184+
return null;
185+
}
186+
catch (IOException ex)
187+
{
188+
Log.Error($"{pathSource} SettingsFolderLocation caused an I/O error: {path}. Falling back to {fallbackMessage}.", ex);
189+
return null;
190+
}
181191
}
182192

183193
/// <summary>

Source/NETworkManager/App.xaml.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,22 @@ by BornToBeRoot
8787
PolicyManager.Load();
8888

8989
// Load (or initialize) local settings
90-
LocalSettingsManager.Load();
90+
try
91+
{
92+
LocalSettingsManager.Load();
93+
}
94+
catch (IOException ex)
95+
{
96+
Log.Error("Could not load local settings due to an I/O error! Using default local settings.", ex);
97+
}
98+
catch (UnauthorizedAccessException ex)
99+
{
100+
Log.Error("Could not load local settings due to insufficient permissions! Using default local settings.", ex);
101+
}
102+
catch (JsonException ex)
103+
{
104+
Log.Error("Could not load local settings! Local settings JSON is corrupted or invalid. Using default local settings.", ex);
105+
}
91106

92107
// Load (or initialize) settings
93108
try

0 commit comments

Comments
 (0)