Skip to content

Commit 1037f9b

Browse files
authored
Fixed app crashing when parsing terminal.json file (#1552)
1 parent 924464a commit 1037f9b

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

Files/Controllers/TerminalController.cs

+17-3
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,25 @@ private async Task Load()
4747
if (Model == null)
4848
{
4949
Model = new TerminalFileModel();
50-
throw new JsonSerializationException($"{JsonFileName} is empty, regenerating...");
50+
throw new JsonParsingNullException(JsonFileName);
5151
}
5252
}
53-
catch (JsonSerializationException)
53+
catch (JsonParsingNullException)
5454
{
5555
var defaultFile = StorageFile.GetFileFromApplicationUriAsync(new Uri(defaultTerminalPath));
56-
56+
5757
JsonFile = await Folder.CreateFileAsync(JsonFileName, CreationCollisionOption.ReplaceExisting);
5858
await FileIO.WriteBufferAsync(JsonFile, await FileIO.ReadBufferAsync(await defaultFile));
5959
var defaultContent = await FileIO.ReadTextAsync(JsonFile);
6060
Model = JsonConvert.DeserializeObject<TerminalFileModel>(defaultContent);
6161
}
62+
catch (Exception)
63+
{
64+
var defaultFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(defaultTerminalPath));
65+
JsonFile = null;
66+
var defaultContent = await FileIO.ReadTextAsync(defaultFile);
67+
Model = JsonConvert.DeserializeObject<TerminalFileModel>(defaultContent);
68+
}
6269
}
6370

6471
public async void Init()
@@ -95,6 +102,8 @@ public async Task GetInstalledTerminals()
95102

96103
public void SaveModel()
97104
{
105+
if (JsonFile == null) return;
106+
98107
using (var file = File.CreateText(Folder.Path + Path.DirectorySeparatorChar + JsonFileName))
99108
{
100109
JsonSerializer serializer = new JsonSerializer();
@@ -103,4 +112,9 @@ public void SaveModel()
103112
}
104113
}
105114
}
115+
116+
public class JsonParsingNullException : Exception
117+
{
118+
public JsonParsingNullException(string jsonFileName) : base($"{jsonFileName} is empty, regenerating...") { }
119+
}
106120
}

0 commit comments

Comments
 (0)