~ruther/NosSmooth.Local

8321a10bd9b82a57d35cabb07ed81b35d452f9e8 — Rutherther 3 years ago 4fddd51
feat: correctly handle errors in chat command interceptor
1 files changed, 29 insertions(+), 17 deletions(-)

M src/Extensions/NosSmooth.ChatCommands/ChatCommandInterceptor.cs
M src/Extensions/NosSmooth.ChatCommands/ChatCommandInterceptor.cs => src/Extensions/NosSmooth.ChatCommands/ChatCommandInterceptor.cs +29 -17
@@ 73,27 73,39 @@ public class ChatCommandInterceptor : IPacketInterceptor

    private async Task ExecuteCommand(string command)
    {
        var preparedResult = await _commandService.TryPrepareCommandAsync(command, _serviceProvider);
        if (!preparedResult.IsSuccess)
        try
        {
            _logger.LogError($"Could not prepare \"{command}\"");
            _logger.LogResultError(preparedResult);
            await _feedbackService.SendErrorMessageAsync($"Could not prepare the given command. {preparedResult.ToFullString()}");
        }
            var preparedResult = await _commandService.TryPrepareCommandAsync(command, _serviceProvider);
            if (!preparedResult.IsSuccess)
            {
                _logger.LogError($"Could not prepare \"{command}\"");
                _logger.LogResultError(preparedResult);
                await _feedbackService.SendErrorMessageAsync
                    ($"Could not prepare the given command. {preparedResult.ToFullString()}");
                return;
            }

        var executeResult = await _commandService.TryExecuteAsync(preparedResult.Entity, _serviceProvider);
        if (!executeResult.IsSuccess)
        {
            _logger.LogError($"Could not execute \"{command}\"");
            _logger.LogResultError(executeResult);
            await _feedbackService.SendErrorMessageAsync($"Could not execute the given command. {executeResult.ToFullString()}");
        }
            var executeResult = await _commandService.TryExecuteAsync(preparedResult.Entity, _serviceProvider);
            if (!executeResult.IsSuccess)
            {
                _logger.LogError($"Could not execute \"{command}\"");
                _logger.LogResultError(executeResult);
                await _feedbackService.SendErrorMessageAsync
                    ($"Could not execute the given command. {executeResult.ToFullString()}");
                return;
            }

        if (!executeResult.Entity.IsSuccess)
            if (!executeResult.Entity.IsSuccess)
            {
                _logger.LogError($"There was an error while handling \"{command}\"");
                _logger.LogResultError(executeResult.Entity);
                await _feedbackService.SendErrorMessageAsync
                    ($"Could not execute the given command. {executeResult.Entity.ToFullString()}");
            }
        }
        catch (Exception e)
        {
            _logger.LogError($"There was an error while handling \"{command}\"");
            _logger.LogResultError(executeResult.Entity);
            await _feedbackService.SendErrorMessageAsync($"Could not execute the given command. {executeResult.Entity.ToFullString()}");
            _logger.LogError(e, "Could not execute a command");
        }
    }
}
\ No newline at end of file

Do not follow this link