~ruther/NosSmooth

d9cc716df5adf4d3db0be66e2e21e0e6d3ed66e5 — Rutherther 3 years ago 4b2ede7
feat(core): handle exceptions in command processor
1 files changed, 56 insertions(+), 33 deletions(-)

M Core/NosSmooth.Core/Commands/CommandProcessor.cs
M Core/NosSmooth.Core/Commands/CommandProcessor.cs => Core/NosSmooth.Core/Commands/CommandProcessor.cs +56 -33
@@ 97,7 97,16 @@ public class CommandProcessor
            return result;
        }

        var handlerResult = await commandHandler.HandleCommand(command, ct);
        Result handlerResult;
        try
        {
            handlerResult = await commandHandler.HandleCommand(command, ct);
        }
        catch (Exception e)
        {
            handlerResult = e;
        }

        var afterResult = await ExecuteAfterExecutionAsync
        (
            scope.ServiceProvider,


@@ 129,27 138,34 @@ public class CommandProcessor
    )
        where TCommand : ICommand
    {
        var results = await Task.WhenAll
        (
            services.GetServices<IPreCommandExecutionEvent>()
                .Select(x => x.ExecuteBeforeCommandAsync(client, command, ct))
        );

        var errorResults = new List<Result>();
        foreach (var result in results)
        try
        {
            if (!result.IsSuccess)
            var results = await Task.WhenAll
            (
                services.GetServices<IPreCommandExecutionEvent>()
                    .Select(x => x.ExecuteBeforeCommandAsync(client, command, ct))
            );

            var errorResults = new List<Result>();
            foreach (var result in results)
            {
                errorResults.Add(result);
                if (!result.IsSuccess)
                {
                    errorResults.Add(result);
                }
            }
        }

        return errorResults.Count switch
            return errorResults.Count switch
            {
                1 => errorResults[0],
                0 => Result.FromSuccess(),
                _ => new AggregateError(errorResults.Cast<IResult>().ToArray())
            };
        }
        catch (Exception e)
        {
            1 => errorResults[0],
            0 => Result.FromSuccess(),
            _ => new AggregateError(errorResults.Cast<IResult>().ToArray())
        };
            return e;
        }
    }

    private async Task<Result> ExecuteAfterExecutionAsync<TCommand>


@@ 162,26 178,33 @@ public class CommandProcessor
    )
        where TCommand : ICommand
    {
        var results = await Task.WhenAll
        (
            services.GetServices<IPostCommandExecutionEvent>()
                .Select(x => x.ExecuteAfterCommandAsync(client, command, handlerResult, ct))
        );

        var errorResults = new List<Result>();
        foreach (var result in results)
        try
        {
            if (!result.IsSuccess)
            var results = await Task.WhenAll
            (
                services.GetServices<IPostCommandExecutionEvent>()
                    .Select(x => x.ExecuteAfterCommandAsync(client, command, handlerResult, ct))
            );

            var errorResults = new List<Result>();
            foreach (var result in results)
            {
                errorResults.Add(result);
                if (!result.IsSuccess)
                {
                    errorResults.Add(result);
                }
            }
        }

        return errorResults.Count switch
            return errorResults.Count switch
            {
                1 => errorResults[0],
                0 => Result.FromSuccess(),
                _ => new AggregateError(errorResults.Cast<IResult>().ToArray())
            };
        }
        catch (Exception e)
        {
            1 => errorResults[0],
            0 => Result.FromSuccess(),
            _ => new AggregateError(errorResults.Cast<IResult>().ToArray())
        };
            return e;
        }
    }
}
\ No newline at end of file

Do not follow this link