Improve error handling

rendering v3.3
Liza 2023-02-24 11:18:03 +01:00
parent 17f7dcdf12
commit 3663bbb241
4 changed files with 42 additions and 21 deletions

View File

@ -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)
{

View File

@ -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<T> logger = scope.ServiceProvider.GetRequiredService<ILogger<T>>();
using var dbContext = scope.ServiceProvider.GetRequiredService<PalClientContext>();
try
{
using var scope = _serviceScopeFactory.CreateScope();
ILogger<T> logger = scope.ServiceProvider.GetRequiredService<ILogger<T>>();
using var dbContext = scope.ServiceProvider.GetRequiredService<PalClientContext>();
Run(dbContext, logger);
Run(dbContext, logger);
}
catch (Exception e)
{
DependencyInjectionContext.LoggerProvider.CreateLogger<DbTask<T>>()
.LogError(e, "Failed to run DbTask");
}
});
}

View File

@ -142,9 +142,12 @@ namespace Pal.Client
Task.Run(async () =>
{
IServiceScope rootScope;
Chat chat;
try
{
rootScope = await _rootScopeCompletionSource.Task;
chat = rootScope.ServiceProvider.GetRequiredService<Chat>();
}
catch (Exception e)
{
@ -152,17 +155,16 @@ namespace Pal.Client
return;
}
IPalacePalConfiguration configuration =
rootScope.ServiceProvider.GetRequiredService<IPalacePalConfiguration>();
Chat chat = rootScope.ServiceProvider.GetRequiredService<Chat>();
if (configuration.FirstUse && arguments != "" && arguments != "config")
{
chat.Error(Localization.Error_FirstTimeSetupRequired);
return;
}
try
{
IPalacePalConfiguration configuration =
rootScope.ServiceProvider.GetRequiredService<IPalacePalConfiguration>();
if (configuration.FirstUse && arguments != "" && arguments != "config")
{
chat.Error(Localization.Error_FirstTimeSetupRequired);
return;
}
var sp = rootScope.ServiceProvider;
switch (arguments)

View File

@ -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)