Browse Source

Error handling for malformed preference names

CPKreuz 1 year ago
parent
commit
05ef3aecd2

+ 2 - 0
src/PixiEditor.Extensions.CommonApi/UserPreferences/Settings/Setting.cs

@@ -37,6 +37,8 @@ public abstract class Setting<T> : INotifyPropertyChanged
     /// <param name="fallbackValue">The value used if the preference has not been set before</param>
     /// <param name="fallbackValue">The value used if the preference has not been set before</param>
     protected Setting(string name, T? fallbackValue = default)
     protected Setting(string name, T? fallbackValue = default)
     {
     {
+        SettingHelper.ThrowIfEmptySettingName(name);
+        
         Name = name;
         Name = name;
         FallbackValue = fallbackValue;
         FallbackValue = fallbackValue;
         
         

+ 0 - 16
src/PixiEditor.Extensions.CommonApi/UserPreferences/Settings/SettingExtensions.cs

@@ -1,16 +0,0 @@
-namespace PixiEditor.Extensions.CommonApi.UserPreferences.Settings;
-
-public static class SettingExtensions
-{
-    public static List<T> AsList<T>(this Setting<IEnumerable<T>> setting) =>
-        setting.As(new List<T>());
-    
-    public static T[] AsArray<T>(this Setting<IEnumerable<T>> setting) =>
-        setting.As(Array.Empty<T>());
-
-    public static void AddListCallback<T>(this Setting<IEnumerable<T>> setting, Action<List<T>> callback) =>
-        setting.ValueChanged += (_, value) => callback(value.ToList());
-
-    public static void AddArrayCallback<T>(this Setting<IEnumerable<T>> setting, Action<T[]> callback) =>
-        setting.ValueChanged += (_, value) => callback(value.ToArray());
-}

+ 40 - 0
src/PixiEditor.Extensions.CommonApi/UserPreferences/Settings/SettingHelpers.cs

@@ -0,0 +1,40 @@
+using System.Diagnostics;
+
+namespace PixiEditor.Extensions.CommonApi.UserPreferences.Settings;
+
+public static class SettingHelper
+{
+    public static List<T> AsList<T>(this Setting<IEnumerable<T>> setting) =>
+        setting.As(new List<T>());
+    
+    public static T[] AsArray<T>(this Setting<IEnumerable<T>> setting) =>
+        setting.As(Array.Empty<T>());
+
+    public static void AddListCallback<T>(this Setting<IEnumerable<T>> setting, Action<List<T>> callback) =>
+        setting.ValueChanged += (_, value) => callback(value.ToList());
+
+    public static void AddArrayCallback<T>(this Setting<IEnumerable<T>> setting, Action<T[]> callback) =>
+        setting.ValueChanged += (_, value) => callback(value.ToArray());
+    
+    [StackTraceHidden]
+    public static void ThrowIfEmptySettingName(string name)
+    {
+        if (string.IsNullOrEmpty(name)) {
+            throw new ArgumentException($"name was empty", nameof(name));
+        }
+    
+        var colon = name.IndexOf(':');
+    
+        if (colon == 0)
+        {
+            // ":<any key>" does not have a valid prefix
+            throw new ArgumentException($"The prefix in the name '{name}' was empty", nameof(name));
+        }
+
+        if (colon == name.Length - 1)
+        {
+            // "<any prefix>:" does not have a valid key
+            throw new ArgumentException($"The key in the name '{name}' was empty", nameof(name));
+        }
+    }
+}