Editor IO exception handling (#1491)
* Close input stream Show MessageBox with exception message Remove unused arguments Guard all of the Editor input/output code parts and show MessageBox with appropriate message and issue reporting link * Extract showing messageBox into method
This commit is contained in:
parent
52e08a2784
commit
5581e25a21
|
@ -16,7 +16,7 @@ namespace FancyZonesEditor
|
|||
EditorOverlay mainEditor = EditorOverlay.Current;
|
||||
if (mainEditor.DataContext is LayoutModel model)
|
||||
{
|
||||
model.Persist(mainEditor.GetZoneRects());
|
||||
model.Persist();
|
||||
}
|
||||
|
||||
_choosing = true;
|
||||
|
|
|
@ -150,11 +150,11 @@ namespace FancyZonesEditor
|
|||
{
|
||||
if (model is GridLayoutModel)
|
||||
{
|
||||
model.Apply(mainEditor.GetZoneRects());
|
||||
model.Apply();
|
||||
}
|
||||
else
|
||||
{
|
||||
model.Apply((model as CanvasLayoutModel).Zones.ToArray());
|
||||
model.Apply();
|
||||
}
|
||||
|
||||
Close();
|
||||
|
|
|
@ -116,6 +116,8 @@ namespace FancyZonesEditor.Models
|
|||
// PersistData
|
||||
// Implements the LayoutModel.PersistData abstract method
|
||||
protected override void PersistData()
|
||||
{
|
||||
try
|
||||
{
|
||||
FileStream outputStream = File.Open(Settings.AppliedZoneSetTmpFile, FileMode.Create);
|
||||
JsonWriterOptions writerOptions = new JsonWriterOptions
|
||||
|
@ -158,5 +160,10 @@ namespace FancyZonesEditor.Models
|
|||
|
||||
outputStream.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ShowExceptionMessageBox("Error persisting canvas layout", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,10 +2,11 @@
|
|||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Windows;
|
||||
|
||||
namespace FancyZonesEditor.Models
|
||||
{
|
||||
|
@ -169,6 +170,8 @@ namespace FancyZonesEditor.Models
|
|||
// PersistData
|
||||
// Implements the LayoutModel.PersistData abstract method
|
||||
protected override void PersistData()
|
||||
{
|
||||
try
|
||||
{
|
||||
FileStream outputStream = File.Open(Settings.AppliedZoneSetTmpFile, FileMode.Create);
|
||||
using (var writer = new Utf8JsonWriter(outputStream, options: default))
|
||||
|
@ -224,5 +227,10 @@ namespace FancyZonesEditor.Models
|
|||
|
||||
outputStream.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ShowExceptionMessageBox("Error persisting grid layout", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,8 +27,12 @@ namespace FancyZonesEditor.Models
|
|||
// Manages common properties and base persistence
|
||||
public abstract class LayoutModel : INotifyPropertyChanged
|
||||
{
|
||||
private static readonly string _registryPath = Settings.RegistryPath + "\\Layouts";
|
||||
private static readonly string _fullRegistryPath = Settings.FullRegistryPath + "\\Layouts";
|
||||
public static void ShowExceptionMessageBox(string message, Exception ex)
|
||||
{
|
||||
string title = "FancyZones Editor Exception Handler";
|
||||
string fullMessage = "Please report the bug to https://github.com/microsoft/PowerToys/issues \n" + message + ": " + ex.Message;
|
||||
MessageBox.Show(fullMessage, title);
|
||||
}
|
||||
|
||||
protected LayoutModel()
|
||||
{
|
||||
|
@ -132,6 +136,8 @@ namespace FancyZonesEditor.Models
|
|||
}
|
||||
|
||||
public static void SerializeDeletedCustomZoneSets()
|
||||
{
|
||||
try
|
||||
{
|
||||
FileStream outputStream = File.Open(Settings.CustomZoneSetsTmpFile, FileMode.Create);
|
||||
var writer = new Utf8JsonWriter(outputStream, options: default);
|
||||
|
@ -147,24 +153,23 @@ namespace FancyZonesEditor.Models
|
|||
writer.Flush();
|
||||
outputStream.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ShowExceptionMessageBox("Error serializing deleted layouts", ex);
|
||||
}
|
||||
}
|
||||
|
||||
// Loads all the custom Layouts from tmp file passed by FancuZonesLib
|
||||
public static ObservableCollection<LayoutModel> LoadCustomModels()
|
||||
{
|
||||
_customModels = new ObservableCollection<LayoutModel>();
|
||||
|
||||
FileStream inputStream = File.Open(Settings.CustomZoneSetsTmpFile, FileMode.Open);
|
||||
JsonDocument jsonObject;
|
||||
try
|
||||
{
|
||||
jsonObject = JsonDocument.Parse(inputStream, options: default);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return _customModels;
|
||||
}
|
||||
|
||||
FileStream inputStream = File.Open(Settings.CustomZoneSetsTmpFile, FileMode.Open);
|
||||
JsonDocument jsonObject = JsonDocument.Parse(inputStream, options: default);
|
||||
JsonElement.ArrayEnumerator customZoneSetsEnumerator = jsonObject.RootElement.GetProperty("custom-zone-sets").EnumerateArray();
|
||||
|
||||
while (customZoneSetsEnumerator.MoveNext())
|
||||
{
|
||||
var current = customZoneSetsEnumerator.Current;
|
||||
|
@ -228,6 +233,14 @@ namespace FancyZonesEditor.Models
|
|||
}
|
||||
}
|
||||
|
||||
inputStream.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ShowExceptionMessageBox("Error loading custom layouts", ex);
|
||||
return new ObservableCollection<LayoutModel>();
|
||||
}
|
||||
|
||||
return _customModels;
|
||||
}
|
||||
|
||||
|
@ -239,15 +252,16 @@ namespace FancyZonesEditor.Models
|
|||
|
||||
public abstract LayoutModel Clone();
|
||||
|
||||
public void Persist(System.Windows.Int32Rect[] zones)
|
||||
public void Persist()
|
||||
{
|
||||
PersistData();
|
||||
Apply(zones);
|
||||
Apply();
|
||||
}
|
||||
|
||||
public void Apply(System.Windows.Int32Rect[] zones)
|
||||
public void Apply()
|
||||
{
|
||||
try
|
||||
{
|
||||
int zoneCount = zones.Length;
|
||||
FileStream outputStream = File.Open(Settings.ActiveZoneSetTmpFile, FileMode.Create);
|
||||
var writer = new Utf8JsonWriter(outputStream, options: default);
|
||||
|
||||
|
@ -290,5 +304,10 @@ namespace FancyZonesEditor.Models
|
|||
writer.Flush();
|
||||
outputStream.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ShowExceptionMessageBox("Error applying layout", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -359,6 +359,8 @@ namespace FancyZonesEditor
|
|||
}
|
||||
|
||||
private void ParseDeviceInfoData()
|
||||
{
|
||||
try
|
||||
{
|
||||
FileStream inputStream = File.Open(Settings.ActiveZoneSetTmpFile, FileMode.Open);
|
||||
var jsonObject = JsonDocument.Parse(inputStream, options: default).RootElement;
|
||||
|
@ -403,6 +405,12 @@ namespace FancyZonesEditor
|
|||
_spacing = jsonObject.GetProperty("editor-spacing").GetInt32();
|
||||
_zoneCount = jsonObject.GetProperty("editor-zone-count").GetInt32();
|
||||
}
|
||||
|
||||
inputStream.Close();
|
||||
} catch (Exception ex)
|
||||
{
|
||||
LayoutModel.ShowExceptionMessageBox("Error parsing device info data", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void ParseCommandLineArgs()
|
||||
|
|
Loading…
Reference in a new issue