Improve error handling if a command fails with an exception

This commit is contained in:
Liza 2023-02-25 01:15:19 +01:00
parent 0c922c1695
commit 18ddd32f25
4 changed files with 29 additions and 4 deletions

View File

@ -2,6 +2,7 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Dalamud.Game.Gui; using Dalamud.Game.Gui;
using Grpc.Core; using Grpc.Core;
using Microsoft.Extensions.Logging;
using Pal.Client.Configuration; using Pal.Client.Configuration;
using Pal.Client.Extensions; using Pal.Client.Extensions;
using Pal.Client.Net; using Pal.Client.Net;
@ -13,14 +14,20 @@ namespace Pal.Client.DependencyInjection
internal sealed class StatisticsService internal sealed class StatisticsService
{ {
private readonly IPalacePalConfiguration _configuration; private readonly IPalacePalConfiguration _configuration;
private readonly ILogger<StatisticsService> _logger;
private readonly RemoteApi _remoteApi; private readonly RemoteApi _remoteApi;
private readonly StatisticsWindow _statisticsWindow; private readonly StatisticsWindow _statisticsWindow;
private readonly Chat _chat; private readonly Chat _chat;
public StatisticsService(IPalacePalConfiguration configuration, RemoteApi remoteApi, public StatisticsService(
StatisticsWindow statisticsWindow, Chat chat) IPalacePalConfiguration configuration,
ILogger<StatisticsService> logger,
RemoteApi remoteApi,
StatisticsWindow statisticsWindow,
Chat chat)
{ {
_configuration = configuration; _configuration = configuration;
_logger = logger;
_remoteApi = remoteApi; _remoteApi = remoteApi;
_statisticsWindow = statisticsWindow; _statisticsWindow = statisticsWindow;
_chat = chat; _chat = chat;
@ -54,11 +61,14 @@ namespace Pal.Client.DependencyInjection
} }
catch (RpcException e) when (e.StatusCode == StatusCode.PermissionDenied) catch (RpcException e) when (e.StatusCode == StatusCode.PermissionDenied)
{ {
_logger.LogWarning(e, "Access denied while fetching floor statistics");
_chat.Error(Localization.Command_pal_stats_CurrentFloor); _chat.Error(Localization.Command_pal_stats_CurrentFloor);
} }
catch (Exception e) catch (Exception e)
{ {
_chat.Error(e.ToString()); _logger.LogError(e, "Could not fetch floor statistics");
_chat.Error(string.Format(Localization.Error_CommandFailed,
$"{e.GetType()} - {e.Message}"));
} }
} }
} }

View File

@ -187,7 +187,8 @@ namespace Pal.Client
{ {
_logger.LogError(e, "Could not execute command '{Command}' with arguments '{Arguments}'", command, _logger.LogError(e, "Could not execute command '{Command}' with arguments '{Arguments}'", command,
arguments); arguments);
chat.Error(e.ToString()); chat.Error(string.Format(Localization.Error_CommandFailed,
$"{e.GetType()} - {e.Message}"));
} }
}); });
} }

View File

@ -619,6 +619,15 @@ namespace Pal.Client.Properties {
} }
} }
/// <summary>
/// Looks up a localized string similar to Command could not be executed: {0}.
/// </summary>
internal static string Error_CommandFailed {
get {
return ResourceManager.GetString("Error_CommandFailed", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to Please finish the initial setup first.. /// Looks up a localized string similar to Please finish the initial setup first..
/// </summary> /// </summary>

View File

@ -48,6 +48,7 @@
</data> </data>
<data name="Error_LoadFailed" xml:space="preserve"> <data name="Error_LoadFailed" xml:space="preserve">
<value>Plugin could not be loaded: {0}</value> <value>Plugin could not be loaded: {0}</value>
<comment>Shown when the plugin fails to load, with the placeholder filled with the exception message.</comment>
</data> </data>
<data name="Error_WrongRepository" xml:space="preserve"> <data name="Error_WrongRepository" xml:space="preserve">
<value>Please install this plugin from the official repository at {0} to continue using it.</value> <value>Please install this plugin from the official repository at {0} to continue using it.</value>
@ -69,6 +70,10 @@
<value>Unable to fetch statistics.</value> <value>Unable to fetch statistics.</value>
<comment>Shown when /pal stats produces a server-side error, and the statistics window can't be loaded.</comment> <comment>Shown when /pal stats produces a server-side error, and the statistics window can't be loaded.</comment>
</data> </data>
<data name="Error_CommandFailed" xml:space="preserve">
<value>Command could not be executed: {0}</value>
<comment>Shown when '/pal ...' fails, with the placeholder filled with the exception message.</comment>
</data>
<!-- Messages while connecting to the server. These are typically only visible when clicking 'Test connection'. --> <!-- Messages while connecting to the server. These are typically only visible when clicking 'Test connection'. -->
<data name="ConnectionSuccessful" xml:space="preserve"> <data name="ConnectionSuccessful" xml:space="preserve">