Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 5cff6a6

Browse files
authored
Merge pull request #521 from github/fixes/516-handle-bad-json-gracefully
Handle IO and json exceptions gracefully
2 parents 987c6f8 + 4788780 commit 5cff6a6

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

src/GitHub.Exports/Services/UsageTracker.cs

+31-5
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,25 @@ public UsageTracker(
4242
[Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider)
4343
{
4444
fileExists = (path) => System.IO.File.Exists(path);
45-
readAllText = (path, encoding) => System.IO.File.ReadAllText(path, encoding);
46-
writeAllText = (path, content, encoding) => System.IO.File.WriteAllText(path, content, encoding);
45+
readAllText = (path, encoding) =>
46+
{
47+
try
48+
{
49+
return System.IO.File.ReadAllText(path, encoding);
50+
}
51+
catch
52+
{
53+
return null;
54+
}
55+
};
56+
writeAllText = (path, content, encoding) =>
57+
{
58+
try
59+
{
60+
System.IO.File.WriteAllText(path, content, encoding);
61+
}
62+
catch {}
63+
};
4764
dirCreate = (path) => System.IO.Directory.CreateDirectory(path);
4865

4966
this.connectionManager = connectionManager;
@@ -138,9 +155,18 @@ public void IncrementLoginCount()
138155

139156
UsageStore LoadUsage()
140157
{
141-
var result = fileExists(storePath) ?
142-
SimpleJson.DeserializeObject<UsageStore>(readAllText(storePath, Encoding.UTF8)) :
143-
new UsageStore { Model = new UsageModel() };
158+
var json = fileExists(storePath) ? readAllText(storePath, Encoding.UTF8) : null;
159+
UsageStore result = null;
160+
try
161+
{
162+
result = json != null ?
163+
SimpleJson.DeserializeObject<UsageStore>(json) :
164+
new UsageStore { Model = new UsageModel() };
165+
}
166+
catch
167+
{
168+
result = new UsageStore { Model = new UsageModel() };
169+
}
144170

145171
result.Model.Lang = CultureInfo.InstalledUICulture.IetfLanguageTag;
146172
result.Model.AppVersion = AssemblyVersionInformation.Version;

0 commit comments

Comments
 (0)