From 3663bbb2415fea901dd121ffd717d9a88a167d34 Mon Sep 17 00:00:00 2001 From: Liza Carvelli Date: Fri, 24 Feb 2023 11:18:03 +0100 Subject: [PATCH] Improve error handling --- .../DependencyInjection/StatisticsService.cs | 12 +++++------ Pal.Client/Floors/Tasks/DbTask.cs | 19 +++++++++++++----- Pal.Client/Plugin.cs | 20 ++++++++++--------- Pal.Client/Windows/ConfigWindow.cs | 12 ++++++++++- 4 files changed, 42 insertions(+), 21 deletions(-) diff --git a/Pal.Client/DependencyInjection/StatisticsService.cs b/Pal.Client/DependencyInjection/StatisticsService.cs index 2096ba8..9b9a029 100644 --- a/Pal.Client/DependencyInjection/StatisticsService.cs +++ b/Pal.Client/DependencyInjection/StatisticsService.cs @@ -33,14 +33,14 @@ namespace Pal.Client.DependencyInjection private async Task FetchFloorStatistics() { - if (!_configuration.HasRoleOnCurrentServer(RemoteApi.RemoteUrl, "statistics:view")) - { - _chat.Error(Localization.Command_pal_stats_CurrentFloor); - return; - } - try { + if (!_configuration.HasRoleOnCurrentServer(RemoteApi.RemoteUrl, "statistics:view")) + { + _chat.Error(Localization.Command_pal_stats_CurrentFloor); + return; + } + var (success, floorStatistics) = await _remoteApi.FetchStatistics(); if (success) { diff --git a/Pal.Client/Floors/Tasks/DbTask.cs b/Pal.Client/Floors/Tasks/DbTask.cs index 64074fe..0d8a6c9 100644 --- a/Pal.Client/Floors/Tasks/DbTask.cs +++ b/Pal.Client/Floors/Tasks/DbTask.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Pal.Client.Database; @@ -19,11 +20,19 @@ namespace Pal.Client.Floors.Tasks { Task.Run(() => { - using var scope = _serviceScopeFactory.CreateScope(); - ILogger logger = scope.ServiceProvider.GetRequiredService>(); - using var dbContext = scope.ServiceProvider.GetRequiredService(); + try + { + using var scope = _serviceScopeFactory.CreateScope(); + ILogger logger = scope.ServiceProvider.GetRequiredService>(); + using var dbContext = scope.ServiceProvider.GetRequiredService(); - Run(dbContext, logger); + Run(dbContext, logger); + } + catch (Exception e) + { + DependencyInjectionContext.LoggerProvider.CreateLogger>() + .LogError(e, "Failed to run DbTask"); + } }); } diff --git a/Pal.Client/Plugin.cs b/Pal.Client/Plugin.cs index c6938d1..e1409dd 100644 --- a/Pal.Client/Plugin.cs +++ b/Pal.Client/Plugin.cs @@ -142,9 +142,12 @@ namespace Pal.Client Task.Run(async () => { IServiceScope rootScope; + Chat chat; + try { rootScope = await _rootScopeCompletionSource.Task; + chat = rootScope.ServiceProvider.GetRequiredService(); } catch (Exception e) { @@ -152,17 +155,16 @@ namespace Pal.Client return; } - IPalacePalConfiguration configuration = - rootScope.ServiceProvider.GetRequiredService(); - Chat chat = rootScope.ServiceProvider.GetRequiredService(); - if (configuration.FirstUse && arguments != "" && arguments != "config") - { - chat.Error(Localization.Error_FirstTimeSetupRequired); - return; - } - try { + IPalacePalConfiguration configuration = + rootScope.ServiceProvider.GetRequiredService(); + if (configuration.FirstUse && arguments != "" && arguments != "config") + { + chat.Error(Localization.Error_FirstTimeSetupRequired); + return; + } + var sp = rootScope.ServiceProvider; switch (arguments) diff --git a/Pal.Client/Windows/ConfigWindow.cs b/Pal.Client/Windows/ConfigWindow.cs index c3b279b..a23b8d9 100644 --- a/Pal.Client/Windows/ConfigWindow.cs +++ b/Pal.Client/Windows/ConfigWindow.cs @@ -472,7 +472,17 @@ namespace Pal.Client.Windows CancellationTokenSource cts = new CancellationTokenSource(); _lastImportCts = cts; - Task.Run(async () => { _lastImport = await _importService.FindLast(cts.Token); }, cts.Token); + Task.Run(async () => + { + try + { + _lastImport = await _importService.FindLast(cts.Token); + } + catch (Exception e) + { + _logger.LogError(e, "Unable to fetch last import"); + } + }, cts.Token); } private void DoExport(string destinationPath)