From 42d95c2e729bf3c8ed147e7ef1099b4dbc1d77b2 Mon Sep 17 00:00:00 2001 From: Fan Yang <52458914+fanyang-mono@users.noreply.github.com> Date: Fri, 6 Aug 2021 02:15:01 -0400 Subject: [PATCH] Improve error handling for task RuntimeConfigParser (#56942) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * Improve error handling * Update src/tasks/RuntimeConfigParser/RuntimeConfigParser.cs Co-authored-by: Aleksey Kliger (λgeek) * Update src/tasks/RuntimeConfigParser/RuntimeConfigParser.cs Co-authored-by: Aleksey Kliger (λgeek) * Fix error Co-authored-by: Aleksey Kliger (λgeek) --- .../RuntimeConfigParser/RuntimeConfigParser.cs | 29 ++++++++++++++++------ 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/tasks/RuntimeConfigParser/RuntimeConfigParser.cs b/src/tasks/RuntimeConfigParser/RuntimeConfigParser.cs index d0b7ec1..ade4d1c 100644 --- a/src/tasks/RuntimeConfigParser/RuntimeConfigParser.cs +++ b/src/tasks/RuntimeConfigParser/RuntimeConfigParser.cs @@ -34,14 +34,19 @@ public class RuntimeConfigParserTask : Task if (string.IsNullOrEmpty(RuntimeConfigFile)) { Log.LogError($"'{nameof(RuntimeConfigFile)}' is required."); + return false; } if (string.IsNullOrEmpty(OutputFile)) { Log.LogError($"'{nameof(OutputFile)}' is required."); + return false; } - Dictionary configProperties = ConvertInputToDictionary(RuntimeConfigFile); + if (!TryConvertInputToDictionary(RuntimeConfigFile, out Dictionary configProperties)) + { + return false; + } if (RuntimeConfigReservedProperties.Length != 0) { @@ -59,8 +64,12 @@ public class RuntimeConfigParserTask : Task } /// Reads a json file from the given path and extracts the "configProperties" key (assumed to be a string to string dictionary) - private Dictionary ConvertInputToDictionary(string inputFilePath) + private bool TryConvertInputToDictionary(string inputFilePath, out Dictionary result) { + var init_result = new Dictionary(); + init_result.Clear(); + result = init_result; + var options = new JsonSerializerOptions { AllowTrailingCommas = true, ReadCommentHandling = JsonCommentHandling.Skip, @@ -75,18 +84,22 @@ public class RuntimeConfigParserTask : Task if (parsedJson == null) { - throw new ArgumentException("Wasn't able to parse the json file successfully."); + Log.LogError("Wasn't able to parse the json file successfully."); + return false; } if (parsedJson.RuntimeOptions == null) { - throw new ArgumentException("Key runtimeOptions wasn't found in the json file."); + Log.LogError("Key runtimeOptions wasn't found in the json file."); + return false; } if (parsedJson.RuntimeOptions.ConfigProperties == null) { - throw new ArgumentException("Key runtimeOptions->configProperties wasn't found in the json file."); + Log.LogError("Key runtimeOptions->configProperties wasn't found in the json file."); + return false; } - return parsedJson.RuntimeOptions.ConfigProperties; + result = parsedJson.RuntimeOptions.ConfigProperties; + return true; } /// Just write the dictionary out to a blob as a count followed by @@ -109,7 +122,7 @@ public class RuntimeConfigParserTask : Task { if (properties.ContainsKey(key.ItemSpec)) { - throw new ArgumentException($"Property '{key}' can't be set by the user!"); + Log.LogError($"Property '{key}' can't be set by the user!"); } } } @@ -166,4 +179,4 @@ public class StringConverter : JsonConverter { writer.WriteStringValue(value); } -} \ No newline at end of file +} -- 2.7.4