Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

feat(domain): wrap reading/writing user config file in a try-catch

Lars Berger3115b1eadc19
diff --git a/LarsWM.Domain/UserConfigs/CommandHandlers/EvaluateUserConfigHandler.cs b/LarsWM.Domain/UserConfigs/CommandHandlers/EvaluateUserConfigHandler.cs
index 3e694874..fcca22db 100644
--- a/LarsWM.Domain/UserConfigs/CommandHandlers/EvaluateUserConfigHandler.cs
+++ b/LarsWM.Domain/UserConfigs/CommandHandlers/EvaluateUserConfigHandler.cs
@@ -1,4 +1,4 @@
-using LarsWM.Domain.UserConfigs.Commands;
+using LarsWM.Domain.UserConfigs.Commands;
using LarsWM.Domain.Workspaces.Commands;
using LarsWM.Infrastructure.Bussing;
using System;
@@ -24,59 +24,74 @@ namespace LarsWM.Domain.UserConfigs.CommandHandlers
public dynamic Handle(EvaluateUserConfigCommand command)
{
- var userConfigPath = _userConfigService.UserConfigPath;
-
- if (!File.Exists(userConfigPath))
- {
- // Initialize the user config with the sample config.
- Directory.CreateDirectory(Path.GetDirectoryName(userConfigPath));
- File.Copy(_userConfigService.SampleUserConfigPath, userConfigPath, false);
- }
-
- var userConfigLines = File.ReadAllLines(userConfigPath);
- var input = new StringReader(string.Join(Environment.NewLine, userConfigLines));
-
- var deserializer = new DeserializerBuilder()
- .WithNamingConvention(PascalCaseNamingConvention.Instance)
- .WithNodeDeserializer(inner => new ValidatingDeserializer(inner), s => s.InsteadOf<ObjectNodeDeserializer>())
- .Build();
-
UserConfigFileDto deserializedConfig = null;
try
{
- deserializedConfig = deserializer.Deserialize<UserConfigFileDto>(input);
- }
- catch (Exception exception)
- {
- var errorMessage = exception.Message;
+ var userConfigPath = _userConfigService.UserConfigPath;
- if (exception.InnerException?.Message != null)
+ if (!File.Exists(userConfigPath))
{
- var unknownPropertyRegex = new Regex(@"Property '(?<property>.*?)' not found on type");
- var match = unknownPropertyRegex.Match(exception.InnerException.Message);
-
- // Improve error message shown in case of unknown property error.
- if (match.Success)
- errorMessage = $"Unknown property in config: {match.Groups["property"]}.";
- else
- errorMessage += $". {exception.InnerException.Message}";
+ // Initialize the user config with the sample config.
+ Directory.CreateDirectory(Path.GetDirectoryName(userConfigPath));
+ File.Copy(_userConfigService.SampleUserConfigPath, userConfigPath, false);
}
- // Alert the user of the config error.
- MessageBox.Show(errorMessage);
-
+ deserializedConfig = DeserializeUserConfig(userConfigPath);
+ }
+ catch (Exception exception)
+ {
+ ShowErrorAlert(exception);
throw exception;
}
+ // Create an inactive `Workspace` for each workspace config.
foreach (var workspaceConfig in deserializedConfig.Workspaces)
_bus.Invoke(new CreateWorkspaceCommand(workspaceConfig.Name));
// TODO: Read user config from file / constructed through shell script.
var userConfig = new UserConfig();
+
_userConfigService.UserConfig = userConfig;
return CommandResponse.Ok;
}
+
+
+ private UserConfigFileDto DeserializeUserConfig(string userConfigPath)
+ {
+ var userConfigLines = File.ReadAllLines(userConfigPath);
+ var input = new StringReader(string.Join(Environment.NewLine, userConfigLines));
+
+ var deserializer = new DeserializerBuilder()
+ .WithNamingConvention(PascalCaseNamingConvention.Instance)
+ .WithNodeDeserializer(
+ inner => new ValidatingDeserializer(inner),
+ component => component.InsteadOf<ObjectNodeDeserializer>()
+ )
+ .Build();
+
+ return deserializer.Deserialize<UserConfigFileDto>(input);
+ }
+
+ private void ShowErrorAlert(Exception exception)
+ {
+ var errorMessage = exception.Message;
+
+ if (exception.InnerException?.Message != null)
+ {
+ var unknownPropertyRegex = new Regex(@"Property '(?<property>.*?)' not found on type");
+ var match = unknownPropertyRegex.Match(exception.InnerException.Message);
+
+ // Improve error message shown in case of unknown property error.
+ if (match.Success)
+ errorMessage = $"Unknown property in config: {match.Groups["property"]}.";
+ else
+ errorMessage += $". {exception.InnerException.Message}";
+ }
+
+ // Alert the user of the config error.
+ MessageBox.Show(errorMessage);
+ }
}
}