From e918ac0d9ba8b22c75835ae93ae4ead78c4cdd4a Mon Sep 17 00:00:00 2001 From: Anjan Nath Date: Fri, 3 Sep 2021 13:26:32 +0530 Subject: [PATCH] Issue #128 Show error notification if correct pull-secret format is not used earlier it was showing a notification as pull secret stored even if daemon returned an error BasicGetCommand and BasicPostCommand now wait for the Result to be returned so Start now waits for setPullSecret to return --- CRCTray.cs | 12 ++++++++---- Communication/DaemonCommander.cs | 31 +++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/CRCTray.cs b/CRCTray.cs index 66a987c..719828a 100755 --- a/CRCTray.cs +++ b/CRCTray.cs @@ -251,10 +251,14 @@ async private void StartMenu_Click(object sender, EventArgs e) TaskHelpers.TryTask(Tasks.SendTelemetry, Actions.EnterPullSecret).NoAwait(); - await TaskHelpers.TryTaskAndNotify(Tasks.SetPullSecret, pullSecretContent, - "Pull Secret stored", - "Pull Secret not stored", - String.Empty); + // Store pullsecret; returns false is not able to + var stored = await TaskHelpers.TryTask(Tasks.SetPullSecret, pullSecretContent); + if(!stored) + { + TrayIcon.NotifyError(@"Pull-secret not stored. Please check if valid format is used."); + return; + } + } TrayIcon.NotifyInfo(@"Starting Cluster"); diff --git a/Communication/DaemonCommander.cs b/Communication/DaemonCommander.cs index 63cb663..e24c5bf 100755 --- a/Communication/DaemonCommander.cs +++ b/Communication/DaemonCommander.cs @@ -17,7 +17,7 @@ static class DaemonCommander private static T getResultsForBasicCommand(string command, int timeout = 20) { - var output = SendBasicCommand(command, timeout); + var output = BasicGetCommand(command, timeout); var options = new JsonSerializerOptions { @@ -110,8 +110,26 @@ public static bool GetPullSecret() public static bool SetPullSecret(string data) { - var result = sendPostRequest(BasicCommands.PullSecret, data, 30); - return true; + try + { + _ = BasicPostCommand(BasicCommands.PullSecret, data, 30); + return true; + } + catch (AggregateException ae) + { + ae.Handle((x) => + { + if (x is APIException) // This we know how to handle. + { + // Marking as handled + return true; + } + + return false; + }); + + return false; + } } public static void PostTelemetryRecord(string action) @@ -124,11 +142,16 @@ public static void PostTelemetryRecord(string action) _ = sendPostRequest(BasicCommands.Telemetry, JsonSerializer.Serialize(tr), 30); } - private static string SendBasicCommand(string command, int timeout) + private static string BasicGetCommand(string command, int timeout) { return sendGetRequest(command, timeout).Result; } + private static string BasicPostCommand(string command, string content, int timeout) + { + return sendPostRequest(command, content, timeout).Result; + } + private static async Task sendGetRequest(string command, int timeout) { return await sendRequest(prepareRequest(HttpMethod.Get,