Skip to content

Fixed app crashing when parsing terminal.json file #1552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions Files/Controllers/TerminalController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,25 @@ private async Task Load()
if (Model == null)
{
Model = new TerminalFileModel();
throw new JsonSerializationException($"{JsonFileName} is empty, regenerating...");
throw new JsonParsingNullException(JsonFileName);
}
}
catch (JsonSerializationException)
catch (JsonParsingNullException)
{
var defaultFile = StorageFile.GetFileFromApplicationUriAsync(new Uri(defaultTerminalPath));

JsonFile = await Folder.CreateFileAsync(JsonFileName, CreationCollisionOption.ReplaceExisting);
await FileIO.WriteBufferAsync(JsonFile, await FileIO.ReadBufferAsync(await defaultFile));
var defaultContent = await FileIO.ReadTextAsync(JsonFile);
Model = JsonConvert.DeserializeObject<TerminalFileModel>(defaultContent);
}
catch (Exception)
{
var defaultFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(defaultTerminalPath));
JsonFile = null;
var defaultContent = await FileIO.ReadTextAsync(defaultFile);
Model = JsonConvert.DeserializeObject<TerminalFileModel>(defaultContent);
}
}

public async void Init()
Expand Down Expand Up @@ -95,6 +102,8 @@ public async Task GetInstalledTerminals()

public void SaveModel()
{
if (JsonFile == null) return;

using (var file = File.CreateText(Folder.Path + Path.DirectorySeparatorChar + JsonFileName))
{
JsonSerializer serializer = new JsonSerializer();
Expand All @@ -103,4 +112,9 @@ public void SaveModel()
}
}
}

public class JsonParsingNullException : Exception
{
public JsonParsingNullException(string jsonFileName) : base($"{jsonFileName} is empty, regenerating...") { }
}
}