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:
stefansjfw 2020-03-09 10:41:06 +01:00 committed by GitHub
parent 52e08a2784
commit 5581e25a21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 274 additions and 232 deletions

View file

@ -16,7 +16,7 @@ namespace FancyZonesEditor
EditorOverlay mainEditor = EditorOverlay.Current;
if (mainEditor.DataContext is LayoutModel model)
{
model.Persist(mainEditor.GetZoneRects());
model.Persist();
}
_choosing = true;

View file

@ -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();

View file

@ -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);
}
}
}
}

View file

@ -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);
}
}
}
}

View file

@ -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);
}
}
}
}

View file

@ -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()