From 1a491fd24c9a68000a64f6c4339d328153f3daef Mon Sep 17 00:00:00 2001 From: Marcin Badurowicz Date: Mon, 6 Apr 2020 21:54:40 +0200 Subject: [PATCH] Add try..catch so the application won't fail on exceptions (fix #16) (#22) Especially about HTTP exceptions, which are ignored and the application is trying another time after a usual sleeping time. --- src/Program.cs | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/src/Program.cs b/src/Program.cs index 0abecf0..34a54f2 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -102,23 +102,46 @@ private static void Main(string[] args) if (isRunSet) { - bot.AnalyzePostsByIdsAsync(runSet).Wait(); + try + { + bot.AnalyzePostsByIdsAsync(runSet).Wait(); + } + catch (AggregateException e) + { + logger.LogError($"HTTP Exception: {e.Message}"); + } return; } if (isRunOnce) { - bot.AnalyzeNewPostsAsync().Wait(); - logger.LogDebug("Single run completed."); + try + { + bot.AnalyzeNewPostsAsync().Wait(); + logger.LogDebug("Single run completed."); + } + catch (AggregateException e) + { + logger.LogError($"HTTP Exception: {e.Message}"); + } } else { while (true) { - bot.AnalyzeNewPostsAsync().Wait(); - logger.LogDebug("Going to sleep for {0} minutes", timeBetweenUpdates); - - Task.Delay(TimeSpan.FromMinutes(timeBetweenUpdates)).Wait(); + try + { + bot.AnalyzeNewPostsAsync().Wait(); + } + catch (AggregateException e) + { + logger.LogError($"HTTP Exception: {e.Message}"); + } + finally + { + logger.LogDebug("Going to sleep for {0} minutes", timeBetweenUpdates); + Task.Delay(TimeSpan.FromMinutes(timeBetweenUpdates)).Wait(); + } } }