From 77f6b3c39dde37a60962a3f1201e3190b5913d93 Mon Sep 17 00:00:00 2001 From: Robin Munn Date: Tue, 19 Nov 2024 17:16:14 +0700 Subject: [PATCH] Do not throw exceptions for empty model number (#1243) Empty projects now just return null. Projects where the model number file is malformed (invalid JSON or whatever) now log the error, then return null without throwing an exception. --- backend/LexBoxApi/Services/HgService.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/backend/LexBoxApi/Services/HgService.cs b/backend/LexBoxApi/Services/HgService.cs index 7155108c1..32e328943 100644 --- a/backend/LexBoxApi/Services/HgService.cs +++ b/backend/LexBoxApi/Services/HgService.cs @@ -271,8 +271,19 @@ private string[] GetWsList(System.Xml.XmlElement root, string tagName) { var result = await ExecuteHgCommandServerCommand(code, "flexmodelversion", token); var text = await result.ReadAsStringAsync(token); - var json = JsonDocument.Parse(text); - return json.RootElement.GetProperty("modelversion").GetInt32(); + if (string.IsNullOrWhiteSpace(text)) return null; + try + { + var json = JsonDocument.Parse(text); + if (json.RootElement.TryGetProperty("modelversion", out var modelversion) && modelversion.TryGetInt32(out int version)) return version; + _logger.LogError("Invalid JSON {text} in GetModelVersionOfFlexProject, should have one property \"modelversion\" that's a number", text); + return null; + } + catch (JsonException e) + { + _logger.LogError("Malformed JSON {text} in GetModelVersionOfFlexProject: {error}", text, e.ToString()); + return null; + } } public Task RevertRepo(ProjectCode code, string revHash)