From 501d33b913e02d880eb19a4f94cb720d9eff365f Mon Sep 17 00:00:00 2001 From: g3gg0 Date: Tue, 13 Feb 2024 12:58:22 +0100 Subject: [PATCH] better dissect exception reasons when dl/loading json --- TeddyBench/TeddyMain.cs | 54 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/TeddyBench/TeddyMain.cs b/TeddyBench/TeddyMain.cs index da047f3..d6b85ce 100644 --- a/TeddyBench/TeddyMain.cs +++ b/TeddyBench/TeddyMain.cs @@ -141,24 +141,68 @@ public void LoadJson(bool force = false) { try { + string jsonContent = ""; + if (Settings.DownloadJson || force) { - TonieInfoString = "| Downloading..."; try { + TonieInfoString = "| Downloading..."; HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.revvox.de/tonies.json?source=TeddyBench&version=" + ThisAssembly.Git.BaseTag); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); TextReader reader = new StreamReader(response.GetResponseStream()); - string content = reader.ReadToEnd(); - File.WriteAllText("tonies.json", content); + jsonContent = reader.ReadToEnd(); TonieInfoString = "| Downloaded"; } catch (Exception e) { TonieInfoString = "| Download Failed"; - ReportException("Downloader Thread", e); + ReportException("Downloader", e); + return; + } + + if (string.IsNullOrEmpty(jsonContent)) + { return; } + bool written = false; + for (int loop = 0; loop < 5; loop++) + { + try + { + File.WriteAllText("tonies.json", jsonContent); + written = true; + break; + } + catch (IOException e) + { + ReportException("Writing tonies.json", e); + Thread.Sleep(100); + } + } + + if (!written) + { + TonieInfoString = "| Writing Failed - is the tonies.json accessible?"; + } + } + else + { + try + { + jsonContent = File.ReadAllText("customTonies.json"); + } + catch (Exception e) + { + TonieInfoString = "| Reading tonies.json Failed"; + TonieInfo = new TonieTools.TonieData[0]; + } + } + + if(string.IsNullOrEmpty(jsonContent)) + { + /* failed to read or download. TonieInfoString is already set */ + return; } lock (TonieInfoLock) @@ -166,7 +210,7 @@ public void LoadJson(bool force = false) try { TonieInfoString = "| Parsing..."; - TonieInfo = JsonConvert.DeserializeObject(File.ReadAllText("tonies.json")); + TonieInfo = JsonConvert.DeserializeObject(jsonContent); TonieInfoString = ""; } catch (Exception e)